Search code examples
gitgit-stash

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"

However, git stash list throws the following error:

fatal: /usr/local/Cellar/git/1.7.4.4/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!

--------UPDATE--------

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:

  1. create a directory where you'd like to store your git repository
  2. create a directory where you want keep the files your going to track (both of these can be anywhere on your file system)
  3. 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.


Solution

  • 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.

    1. 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