Johnny Dew
Johnny Dew

Reputation: 981

git branch -d <branchname> throws error: branch <branchname> not found

When I type git branch, I get

* master
localbranch

But when I try to remove the branch, git branch -d localbranch, I get a not found error:

error: branch 'localbranch' not found.

I have also tried to force the delete with git branch -D localbranch, but it is giving me the same error.

The branch was corrupted and I did the following procedure, Git repository corrupt (incorrect header check; loose object is corrupt), to remove the corrupted files. But now I cannot delete the branch.

Upvotes: 32

Views: 24608

Answers (3)

Tatiana Mamontova
Tatiana Mamontova

Reputation: 11

It helped me to delete the branch not only from .git/refs/head/ but also from .git\refs\remotes Just throughly check for all refs in the .git folder. Then I just fixed up the git's memory with git branch --unset-upstream (since I was removing the branch that was an upstream)

Upvotes: 1

Spangen
Spangen

Reputation: 4730

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).

Upvotes: 14

Anshul Goyal
Anshul Goyal

Reputation: 76907

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

Upvotes: 26

Related Questions