5

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
  • 304
  • 5
  • 16
  • 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
  • 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

2 Answers2

6

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
  • 304
  • 5
  • 16
  • 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
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
  • 48,483
  • 5
  • 42
  • 63
  • ```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