9

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:

  1. Restarting my computer
  2. Reinstalling Git Bash
  3. Checkout to another branch and try git branch -d and git branch -D

Screenshot of error: git branch delete error The (virtualBDD) is my virtual environment. You can ignore that.

AlphaFoxtrot
  • 514
  • 1
  • 6
  • 18
  • Could you post the complete error message screenshot? BTW, did you try restarting your machine? – Mohana Rao Jan 19 '19 at 01:37
  • @MohanaRao The entire error is already in the question I posted but I provided a screenshot if it helps you. I also edited my question to include all the things I have already tried. – AlphaFoxtrot Jan 19 '19 at 17:26
  • 2
    Have you checked out the branch in separate worktree (`git worktree list`)? – joran Jan 20 '19 at 07:01
  • @joran Yes, I have. While I was working in one of my projects, I did ```git worktree add -b branch_name ../project_name develop``` to create another project which branched off of my develop branch. When I do ```git worktree list```, I see the worktree I'm trying to delete. – AlphaFoxtrot Jan 21 '19 at 00:29
  • That sounds fishy. I never used worktree but documentation talks about administrative files being used to track worktree. May be its worth trying suggested cleanup commands from https://git-scm.com/docs/git-worktree – Mohana Rao Jan 21 '19 at 14:49
  • 1
    You may take a look at https://stackoverflow.com/questions/44109234/how-to-delete-a-git-working-tree-branch-when-its-working-directory-has-been-remo – joran Jan 21 '19 at 19:50
  • 1
    Thanks @joran. Doing ```git worktree prune``` solved my issue. Although I read brian's answer first and did a bit more research, you and I essentially came to the same answer. – AlphaFoxtrot Jan 22 '19 at 02:00
  • I had this same problem but my other branch wasn't showing up in `git worktree list` and yet I still couldn't delete it. Finally, I thought to check for any reason the branch would be locked and discovered I was still in the process of an (ugly) rebase. After `git rebase --abort`, I was able to leave the branch and delete it. – Dannid Dec 06 '22 at 19:04

5 Answers5

12

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.

AlphaFoxtrot
  • 514
  • 1
  • 6
  • 18
  • 1
    It is: `git worktree remove`: https://stackoverflow.com/a/49331132/6309 – VonC Jan 22 '19 at 05:48
  • @VonC ```git worktree remove``` did not work for me since I deleted the directory and I kept getting an error stating something along the lines of "That is not a working tree". The link you provided does have the command I needed though. – AlphaFoxtrot Jan 22 '19 at 15:52
4

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.

  1. Are you trying to delete your current branch? 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.
  2. Are you rebasing the branch that you tried to delete? git status will show something about “rebase in progress”. If so, finish rebasing or git rebase --abort.
  3. Are you trying to delete a branch from which you started a git bisect? git status will show “You are currently bisecting, started from branch '<BISECT_START>'.” If so, run git bisect reset.
  4. Are you rebasing with --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).
  5. Repeat steps 1-4 in all your other worktrees (git worktree list) to see why you can’t delete your branch.
yonran
  • 18,156
  • 8
  • 72
  • 97
  • Well, this helped me: just issuing `git status` yielded the information to realised that I am still in the midst of a rebase. – Carsten Greiner Mar 03 '23 at 03:24
  • For me the problem was that I ran `git bisect` in the problematic branch, but forgot to reset the `git bisect`. However I did not receive any descriptive error from `git` sayinc that it is related to `bisect.` – Ardeshir Izadi Aug 24 '23 at 12:13
3

Also is likely that you have a rebase or cherry-pick not resolved in the branch that it is refusing to delete, you have to --abort any of those processes.

git rebase --abort

Then use git branch to see your current branch )it will have swapped to the branch that needed the abort) and git checkout (branchname) to switch branches.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
0

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.

bk2204
  • 64,793
  • 6
  • 84
  • 100
  • ```git worktree remove``` did not work for me since I deleted the directory and I kept getting an error stating something along the lines of "That is not a working tree". I found a method that helped solve my issue that I provided in my own answer to my question. Thanks for your help though. – AlphaFoxtrot Jan 22 '19 at 15:51
  • This is in my answer. Thanks. – AlphaFoxtrot Jan 24 '19 at 16:10
0

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>

Sunil Nalluru
  • 364
  • 2
  • 4