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.
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/headsfor a file named after your branch; if you see one, delete it
Comments
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
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
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
If branch name contains special characters it needs to be quoted:
$ git branch -D 'ENH-Adding-unit-``julian``-to-``to_datetime``'
3 Comments
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?
git branch, or on disk in.git/packed-refsor.git/refs/heads?