Reputation: 684
I'm trying to delete my local branch and tried most of the solutions I found on here which is to checkout to another branch and then run git branch -D (or -d) <my_branch>
. I tried that but I am still getting the same error that states "Cannot delete branch 'my_branch' checked out at 'my_path'
How I got myself in this situation: I branched off of my develop branch by doing git worktree add -b branch_name ../project_name develop
. Then I realized I wanted to change my branch name so I deleted the entire directory first by using rm- rf
. Now my_path is pointing to a deleted directory so I'm not sure what to do now. Help will be appreciated. I'm running on Windows 7 using Git Bash
Things I have tried:
git branch -d
and git branch -D
Screenshot of error: The (virtualBDD) is my virtual environment. You can ignore that.
Upvotes: 23
Views: 40580
Reputation: 457
Came across this strange git behaviour today and was trying hard to get rid of this error. After going through the thread here, following steps helped me.
Upvotes: 2
Reputation: 231
It is also likely that a rebase is currently in progress or a cherry-pick has not been resolved in the branch that it is refusing to delete. In that case you will have to --abort
any of those processes:
git rebase --abort
Then use git branch
to see your current branch (you likely would have moved back to the branch that refused being deleted and was probably the starting point of your rebase) and git checkout <branchname>
to switch branches.
Upvotes: 22
Reputation: 484
You can't delete the check out branch. you need to switch to master or any other branch, then delete the required branch.
git switch master
git branch -d <branch_name>
Upvotes: 2
Reputation: 19134
I ran into this problem today so I looked where the error was being thrown. “Cannot delete branch '<bname>' checked out at '<worktree>'” (builtin/branch.c) is caused by one of the reasons in (branch.c). Basically you should run git status
to find out what applies to you.
git status
will show “On branch <name>”. If so, you have to switch to a different branch first (git switch <other>
), or even switch to a detached HEAD commit.git status
will show something about “rebase in progress”. If so, finish rebasing or git rebase --abort
.git bisect
? git status
will show “You are currently bisecting, started from branch '<BISECT_START>'.” If so, run git bisect reset
.--update-refs
? If so, then all the other branches that were checked out in other worktrees when you started rebasing which use any of the rebasing commits cannot be deleted until you finish or abort the git rebase
. Again, git status
will tell you if you are rebasing, but it does not tell you which other branches update-refs applies to. If you are rebasing, you can find out which branches will be update-refs’d by reading .git/rebase-merge/update-refs
(or .git/worktrees/<worktree>/rebase-merge/update-refs
).git worktree list
) to see why you can’t delete your branch.Upvotes: 17
Reputation: 684
Did a bit more browsing and came across this answer that uses git worktree prune
to remove information on (non-locked) worktrees that no longer exist. This essentially removed my worktree from git worktree list
and then I proceeded to do git branch -d my_branch
. This solved my issue. Thanks to everyone that helped.
Upvotes: 14
Reputation: 76409
You have a worktree for the branch in question checked out in another location and you can't delete that branch until you've removed the worktree.
Git doesn't let you remove a branch that has a worktree associated with it, since that would leave the worktree in a useless and broken state. If you want to delete the branch, you first need to use git worktree remove
to remove the given worktree, possibly with -f
, and then you'll be able to delete the branch. If you're not sure where your worktree is, you can use git worktree list
to find it.
Upvotes: 0