28

I have created branch with "git checkout -b mybranch". It seems that something went wrong, and now I can't delete it using "git branch -D mybranch". It says: error: branch 'mybranch' not found.

5
  • Here's a dumb question: how do you know that it hasn't already been deleted and still exists? Commented Jun 14, 2012 at 8:30
  • 1
    Can you see your branch in git branch, or on disk in .git/packed-refs or .git/refs/heads? Commented Jun 14, 2012 at 8:37
  • 2
    Yes, I see my branch in git branch Commented Jun 15, 2012 at 7:41
  • I had the same issue when working with Terminal, but I found that I'm not in the correct directory (which .git directory is). Commented Dec 22, 2015 at 9:54
  • I had the same issue. To fix it I just closed my terminal and my IDE and reopened them again. It seems the IDE and the terminal entered in conflict somehow and restarting them fixed the issue. Commented Jun 8, 2017 at 8:51

6 Answers 6

65

If git branch -D really isn't working, then your only option is to side-step it and edit the git check-out's state yourself. You should go to the root-directory of your check-out (where the .git directory is) and

  1. edit .git/packed-refs; if you see a line with your branch name then delete it
  2. look in .git/refs/heads for a file named after your branch; if you see one, delete it
Sign up to request clarification or add additional context in comments.

Comments

9

I used git update-ref -d refs/heads/<branch name> to fix this issue. Presumably, this does the same thing as what Rup suggests in the selected answer except it's interfaced via Git's CLI.

Comments

2

I have the same problem git branch -d <branch_name> was not working, And I didn't found anything in .git/packed-refs and .git/refs/heads but I got files in

.git/refs/remotes/origin

with the name of the branches that I was not able to delete locally as well as in remote. But after deleting the files with the branch_name that I wanted to delete it was not showing in local.

To delete it on remote use

git fetch -p origin

The -p --prune option tells that before fetching, remove any remote-tracking references that no longer exist on the remote. Then use command

git push origin :<branch_name_you_was_unable_to_delete>

to delete on remote.

And you are done. :)

Comments

1

You obviously don’t need to delete a branch that does not exist. Use git branch to see a list of branches, if it’s not in there, then there is no branch, and you don’t need to delete it. Otherwise make sure to type the name correctly and git branch -D should work.

Nevertheless you don’t need to care much about a broken branch that might be still around but is inaccessible. Branches in Git are in fact simple 40 bytes files, so not really something you need to worry about.

3 Comments

Also try git branch -a to make sure you haven't accidentally pushed the branch to your remote repository. If you have, you could read instructions on how to delete that branch here
Unless of course you can't push because of a broken/deleted branch that is still in packed-refs.
it's still a pain in the rear seeing broken old branch refs clogging up one's git client. Nobody wants that. Obviously the dude is trying to delete a branch that "appears" to exist having used git branch.
1

I had the same problem. The branch was on the list of branches whenever I executed the git branch command, but I couldn't delete it.

The solution in my case was simple and a bit unexpected: I checked out the broken branch git checkout broken_branch (yes, it worked), then I checked out back to master and... again executed git branch -D broken_branch.

Comments

1

If branch name contains special characters it needs to be quoted:

$ git branch -D 'ENH-Adding-unit-``julian``-to-``to_datetime``'

3 Comments

Thanks for this suggestion. My branch is called sandpit-ş-blockquotes - I tried quoting it as you suggested, but it still didn't work :-( $ git branch -D 'sandpit-ş-blockquotes'. Do you have any more ideas / suggestions?
@samjewell Maybe unicode symbols are not interpreted right by the console?
@samjewell i was able to create and delete branch in SourceTree pp.vk.me/c636719/v636719561/19786/Q5DRz-b39VE.jpg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.