49

I create a worktree for branch using the following command:

Git add worktree <path> <branch-name>

This created a new worktree for me and checked out the branch at the path

Now I thought of deleting/removing the worktree. Ran following commands:

rm -rf <path>
git worktree prune

This should ideally remove the worktree and remove the reference for that path too. So now I should be able to checkout the branch. But when I run following command to checkout to that branch:

git checkout <branch-name>

I get this error:

fatal: '<branch-name>' is already checked out at ''

Could someone help me with what is going on.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Randeep Singh
  • 1,275
  • 14
  • 29

4 Answers4

50

I worked around this by erasing .git/worktrees/<branch-name> from the repository directory.

msteed
  • 524
  • 5
  • 4
  • When using `git worktree` I've added the same name multiple times. The first time I get a ref the same name as the directory. But the second time, because the ref already exists I see a suffixed "1". I wonder if deleting the directory doesn't resolve the reference with the "1" suffixed? This solution helped for me by clearing out the naming discrepency – Ben Simpson Feb 03 '16 at 23:25
  • Of course, you get this fixed by removing the working tree. But this is not the answer to the question: why can't one checkout the same branch in two working trees. – LoKi Feb 04 '16 at 15:37
  • 6
    I think the original question is: why isn't `git prune` removing the worktree administrative files as expected? I don't know the answer to that question, but I did answer the implied question: once my repository is in that state, how can I checkout a new copy of my branch? Also, you can checkout the same branch in two worktrees with `git worktree add -f ...`. – msteed Feb 05 '16 at 00:14
10

I think the right command is:

git worktree prune
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Albatroz
  • 101
  • 1
  • 5
0

Git just likes to be moody sometimes, but what worked for me was first checking which worktrees were still active using this command on git bash

git worktree list

Then you will see all your worktrees and your problem child too, after you see it, look at the branch name it saved because they include the full address of your directory. Copy the whole branch name and paste as such or type it out if that person

git worktree remove <FullBranchName>  (e.g C:/temp/foldername)
sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
-5

A quick fix could be, in case all relevant branches and code are on github. Remove the complete git project directory.

And then clone it again from github, running git clone <github-project>

TIP Getting just the tip of a remote git branch to speed up things

Verhagen
  • 3,885
  • 26
  • 36