Git Stash & Worktree Woes

Issue

I’m having difficulty getting Git to cooperate with my user-defined worktree that exists outside the folder that contains my .git directory.

Basically the setup is like this: I have two directories, one called "git-worktree" which houses the file I want to track & another called "git-dir" which contains the repository’s .git folder aka the GIT_DIR.

I init the repository by cd’ing into folder "git-dir" and running:

git --git-dir=./.git --work-tree=/Users/braitsch/Test/git-worktree/ init

This successfully initializes the repository and while I’m in the "git-dir" folder I can add files that are in the work-tree, run git status and commit them. Cool.

The kick comes when I try to run git stash, which slaps me with the error:

fatal: /usr/local/git/libexec/git-core/git-stash cannot be used without a working tree.

Now this is nonsense, because I know (or at least I think) I have the work-tree set to the "git-worktree" folder that contains the files I want to track.

git config --local -l shows me the following :

core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.worktree=/Users/braitsch/Test/git-worktree
core.ignorecase=true

I even tried manually adding the .git folder to the local config file to see if that helps but to no avail.

core.gitdir=/Users/braitsch/Test/git-dir/.git

Now here comes the kicker. Git stash will work if I set the worktree to a directory that is a parent of the git-dir e.g. If I set the worktree to say my home directory.

core.worktree=/Users/braitsch/

So the question I have is what setting am I missing to tell Git to allow my worktree to exist anywhere on the filesystem, not just along a parent path of the git-dir back up to the root? Surely there must be a way to place your git-dir & your work-tree wherever you want and after telling Git where they are, and have everything work?

Solution

git --git-dir=<your path> --work-tree=<work tree path> stash

should work. Have you tried absolute paths?

UPDATE:

seems to be a bug. The extended functionality of –git-dir option was only added recently and some commands like git stash don’t have the new implementation yet.

git-stash is a bash script. You could hack it and remove the require working dir check then cd into the working folder.

Answered By – Adam Dymitruk

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published