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

时间:2015-02-11 18:47:40

标签: git git-branch

当我输入git branch时,我得到了

* master
localbranch

但是当我尝试删除分支时,git branch -d localbranch我收到了一个未找到的错误:error: branch 'localbranch' not found.

我还尝试用git branch -D localbranch强制删除,但它给了我同样的错误。

分支已损坏,我执行了以下过程Git repository corrupt (incorrect header check; loose object is corrupt),以删除损坏的文件,但现在我无法删除分支。

2 个答案:

答案 0 :(得分:11)

分支存储为包含它们指向的SHA的文件。尝试从项目中的localbranch目录中删除名为.git/refs/head/的此分支的文件:

rm .git/refs/heads/localbranch

答案 1 :(得分:4)

在某些情况下,分支包含未在终端窗口中显示的字符,因此我需要直接访问存储库。

My Git for Windows最终处于这种状态:

$ git branch -l
  master
* next
  my-topic-branch

但删除失败

$ git branch -D my-topic-branch
error: branch 'my-topic-branch' not found.

显示头目录的内容显示分支名称更复杂......

$ 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/

我可以成功删除全名

$ git branch -D ''$'\302\222''my-topic-branch'
Deleted branch my-topic-branch (was efbc2fa).