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.
-
Here's a dumb question: how do you know that it hasn't already been deleted and still exists? – Jun 14 '12 at 08:30
-
1Can you see your branch in `git branch`, or on disk in `.git/packed-refs` or `.git/refs/heads`? – Rup Jun 14 '12 at 08:37
-
2Yes, I see my branch in git branch – User Created Image Jun 15 '12 at 07: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). – Hamed Ghadirian Dec 22 '15 at 09: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. – neftali_af Jun 08 '17 at 08:51
6 Answers
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
- edit
.git/packed-refs
; if you see a line with your branch name then delete it - look in
.git/refs/heads
for a file named after your branch; if you see one, delete it

- 33,765
- 9
- 83
- 112
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.

- 3,399
- 1
- 31
- 28
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. :)

- 861
- 9
- 20
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.

- 369,085
- 72
- 557
- 602
-
1Also 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](http://gitready.com/beginner/2009/02/02/push-and-delete-branches.html) – Shahbaz Jun 14 '12 at 08:57
-
Unless of course you can't push because of a broken/deleted branch that is still in packed-refs. – steviesama Jun 14 '15 at 06:08
-
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. – Max MacLeod Dec 17 '15 at 15:01
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
.

- 2,888
- 2
- 21
- 13
If branch name contains special characters it needs to be quoted:
$ git branch -D 'ENH-Adding-unit-``julian``-to-``to_datetime``'

- 2,093
- 3
- 28
- 48
-
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 Jul 15 '16 at 06:27
-
@samjewell Maybe unicode symbols are not interpreted right by the console? – Winand Jul 15 '16 at 10:14
-
1@samjewell i was able to create and delete branch in SourceTree https://pp.vk.me/c636719/v636719561/19786/Q5DRz-b39VE.jpg – Winand Jul 18 '16 at 08:00