Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 342
Randeep Singh Avatar asked Oct 23 '15 06:10

Randeep Singh


People also ask

How do I force a branch to checkout?

Force a Checkout You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.

What does git Worktree prune do?

git worktree prune removes information about (non-locked) worktrees which no longer exist. For example, $ mkdir a $ cd a $ git init $ git commit --allow-empty --allow-empty-message -m '' $ git worktree add ../b $ ls .git/worktrees/ b $ rm -rf ../b $ git worktree prune $ ls .git/worktrees/ $

Why can't I delete a branch?

Delete a local branch You can't delete a branch if you're checked out that branch. You will see this error: Cannot delete branch 'branch-name' checked out at 'some-location'. To fix this, you will have to switch to a different branch. -d is shortcut for —-delete and it deletes a branch.


1 Answers

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

like image 94
msteed Avatar answered Sep 17 '22 15:09

msteed