git-stash unable to find work tree
I’m trying to setup a git repository with a manually defined worktree via:
cd /Users/braitsch/repos/project1 git --git-dir=. --work-tree=/Users/braitsch/projects/project1 init
After running the above I can add files located in “/Users/braitsch/projects/project1”
via : git add somefile or git add .
Commits work fine as do calls to “git branch”
git stash list throws the following error:
fatal: /usr/local/Cellar/git/22.214.171.124/libexec/git-core/git-stash cannot
be used without a working tree.
Is stashing not supported in user defined work-trees?
git config --local core.worktree echoes out : /Users/braitsch/projects/project1
Any thoughts would be much appreciated!
As @jleedev noted below, there does appear to be a bug when attempting to call “git stash” from outside the worktree. However my workaround is to just cd into the worktree and then call stash by first preceding the path to the gitdir. Inconvenient, I know but the following works for the stash command:
git --git-dir="projects/proj1/.git" stash list
This issue doesn’t appear to plague other stock commands like add, commit, branch, etc. Just “stash” so far as I can tell.
If you are looking to break-away from the default structure of having your .git folder nested inside of your worktree, you might find the following steps useful:
- create a directory where you’d like to store your git repository
- create a directory where you want keep the files your going to track
(both of these can be anywhere on your file system)
cd into your git repository folder and run:
git --git-dir=. --work-tree="path-to-your-project-folder" init
This will init a new repository and link it to your external worktree folder.
To run standard add, delete, branch, commit commands, cd into the your git repository and run your command as usual.
To run stash however, be sure to cd into your worktree and then run stash as I noted above prefacing the command with the path to your gitdir.
3 Solutions collect form web for “git-stash unable to find work tree”
This is either a bug in the command’s behavior or its error reporting. The commands which require a work tree and are implemented as scripts1 verify its presence with this command:
git rev-parse --is-inside-work-tree
which will fail if you are not actually inside the work tree, contrary to what the error message implies. The commands which are implemented in C, on the other hand, call
setup_work_tree, which automatically
chdirs into the work tree. Whether the
require_work_tree function in
git-sh-setup could safely be altered to match this I do not know.
git-am.sh git-bisect.sh git-mergetool.sh git-pull.sh git-rebase--interactive.sh git-rebase.sh git-stash.sh git-submodule.sh
Try setting the GIT_WORK_TREE environment variable to point to suitable folder.
That was a bit odd command. Try this instead. It’s much easier:
mkdir yourRepo cd yourRepo git init .. edit some files git add . git commit -m "First commit"
I ran into this using git for Windows 1.8.3 through ConEmu.
My problem seemed to be related to a drive mapping I had which mapped a path on the
C: drive to
git recognized the path for the mapped drive as
/p at the prompt, but the setting for
git config --local core.worktree was returning
I reset it using
git config --local core.worktree /p, which gave me
p:/ as the new value, but that fixed stash. Seems like it didn’t like the capital drive letter.
Turns out I simply had to
cd /c and then
cd /p to have git notice I had entered the working tree directory. Not sure why git is sensitive to
cd but could not detect I was in the proper place when the shell was initialized.