30

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.

sazzad
  • 5,740
  • 6
  • 25
  • 42
Johnny Dew
  • 971
  • 2
  • 13
  • 29

3 Answers3

23

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
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • 1
    Note that if this branch exists on the remote server as well (though the branch name doesn't suggest so), you could use `git push origin :branchname` to do that. – Anshul Goyal Feb 11 '15 at 18:57
  • that file would only exist if the branch refs were unpacked, it would be better to do a git command that explicitly referenced `refs/heads/localbranch` without using the `.git` directory – Andrew C Feb 11 '15 at 20:10
  • @AndrewC Since this is a local branch, can we assume that it would be unpacked? Also, OP clearly mentioned that `git branch -d localbranch` didn't work for him, so I am curious what local command would work here... Did you mean to try `git branch -d refs/heads/localbranch`? – Anshul Goyal Feb 11 '15 at 20:11
  • @AndrewC `git branch -d refs/heads/localbranch` doesn't work either. – Anshul Goyal Feb 11 '15 at 20:17
  • create a branch with `git branch localname` verify it with `ll .git/refs/heads/localname` run `git gc` then try the `ll` again – Andrew C Feb 11 '15 at 20:35
  • 5
    I would recommend `git update-ref` and only muck around in the .git directory as a last resort noting that branches might be found in `.git/packed-refs` – Andrew C Feb 11 '15 at 20:55
  • Perfect! I found & deleted my problem branch under `.git/refs/remotes/origin` since it was a remote branch that didn't exist remotely nor locally but was still falsely showing up in `git branch -a`. – Gabriel Staples Feb 28 '20 at 02:17
13

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).
Spangen
  • 4,420
  • 5
  • 37
  • 42
1

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)