git branch -d <branchname> throws error: branch <branchname> not found
Branches are stored as files containing the SHA they point to. Try deleting the file for this branch, named localbranch
, from the .git/refs/head/
directory within your project:
rm .git/refs/heads/localbranch
In some cases the branch contains characters that do not display in the terminal window so I needed to go the repository directly.
My Git for Windows ended up in this state:
$ git branch -l
master
* next
my-topic-branch
But removal failed
$ git branch -D my-topic-branch
error: branch 'my-topic-branch' not found.
Showing the contents of the heads directory showed the branch name was more complicated...
$ ls -al .git/refs/heads
total 7
drwxr-xr-x 1 112802 197121 0 Oct 11 13:06 ./
drwxr-xr-x 1 112802 197121 0 Jul 11 14:30 ../
-rw-r--r-- 1 112802 197121 41 Oct 4 12:39 ''$'\302\222''my-topic-branch'
-rw-r--r-- 1 112802 197121 41 Sep 15 15:23 master
-rw-r--r-- 1 112802 197121 41 Oct 11 13:05 next
drwxr-xr-x 1 112802 197121 0 Jul 12 13:28 origin/
And I could successfully delete the full name
$ git branch -D ''$'\302\222''my-topic-branch'
Deleted branch my-topic-branch (was efbc2fa).