Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git-stash unable to find work tree

Tags:

git

git-stash

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.

like image 356
braitsch Avatar asked May 02 '11 22:05

braitsch


People also ask

What is the work tree in Git?

A Git worktree is a linked copy of your Git repository, allowing you to have multiple branches checked out at a time. A worktree has a separate path from your main working copy, but it can be in a different state and on a different branch.

How do I list a Git stash?

Git Stash List. The Git stash list command will pull up a list of your repository's stashes. Git will display all of your stashes and a corresponding stash index. Now, if you wish to view the contents of a specific stash, you can run the Git stash show command followed by stash@ and the desired index.

How to remove worktree in Git?

To remove a locked worktree, specify --force twice. With add , create a new branch named <new-branch> starting at <commit-ish> , and check out <new-branch> into the new worktree. If <commit-ish> is omitted, it defaults to HEAD . By default, -b refuses to create a new branch if it already exists.

How do I stash new files in Git?

The modified untracked files can be saved using the “git stash” command in two different ways. One way is to use the –include-untracked option with the “git stash” command. Another way is to use the -u option with the “git stash” command.


1 Answers

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

like image 107
Josh Lee Avatar answered Oct 17 '22 04:10

Josh Lee