Not able to checkout branch even after removing worktree and running worktree prune
Asked Answered
S

4

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.

Sourpuss answered 23/10, 2015 at 6:35 Comment(7)
What git version are you using?Cappella
I am using the latest version, I think that is 2.5Sourpuss
What does git --version return? 2.5 is not the latest version.Cappella
It's this : git version 2.6.1.windows.1Sourpuss
Ok, I'll check if there is any pending fix for git 2.7Cappella
Did you run git work prune or git worktree prune?Hayott
git worktree prune. I have mistakenly wrote work. edited my questionSourpuss
V
51

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

Vaporization answered 26/10, 2015 at 18:27 Comment(3)
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 discrepencyAnemoscope
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.Snifter
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 ....Vaporization
E
10

I think the right command is:

git worktree prune
Easiness answered 28/3, 2017 at 7:55 Comment(0)
R
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)
Reorganize answered 5/2, 2020 at 10:47 Comment(0)
C
-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

Calamander answered 23/10, 2015 at 7:45 Comment(3)
That is going to take hours because the history of our project is hugeSourpuss
Be sure to reboot three times just in case ;-)Grandnephew
Hello IT, have you tried turning it off and on again?Sevigny

© 2022 - 2024 — McMap. All rights reserved.