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.
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.
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/ $
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.
I worked around this by erasing .git/worktrees/<branch-name>
from the repository directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With