如何在Git中删除“ghost”远程分支?

5

当我执行以下命令:

git branch -a | grep my_funny_branch

返回结果为:

remotes/origin/my_funny_branch

但当我执行以下命令:

git branch -d -r origin/my_funny_branch

会出现以下错误信息:

error: remote branch 'origin/my_funny_branch' not found

而当我执行以下命令:

git pull origin master

就会得到更新的代码。

git pull origin master
From ssh://example.com/foo/bar
 * branch            master     -> FETCH_HEAD
Auto packing the repository for optimum performance. You may also
run "git gc" manually. See "git help gc" for more information.
error: bad ref for refs/remotes/origin/my_funny_branch
error: bad ref for refs/remotes/origin/my_funny_branch
Counting objects: 47339, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (16489/16489), done.
Writing objects: 100% (47339/47339), done.
Total 47339 (delta 30622), reused 47339 (delta 30622)
Rename from '.git/objects/pack/.tmp-7576-pack-15e7c5d209199f384b04dd820a8d625c658f7402.pack' to '.git/objects/pack/pack-15e7c5d209199f384b04dd820a8d625c658f7402.pack' failed. Should I try again? (y/n)

我该如何删除远程分支?

谢谢!

5个回答

14

这个错误信息很有意思:

error: bad ref for refs/remotes/origin/my_funny_branch

查看Git源代码,当Git正在处理该引用的reflog时,会出现该消息。 可能日志以某种方式损坏,导致无法成功完成对引用的各种操作。

备份后,请尝试删除该引用的日志:

rm -rf .git/logs/refs/remotes/origin/my_funny_branch

然后看看你能否删除该分支。


谢谢。结果它突然开始工作了。不确定问题出在哪里。如果我再遇到这个问题,我会尝试这个方法。 - Drew LeSueur
1
这对我有用,但我还必须删除 .git/refs/remotes/origin/my_funny_branch。 - Tim Wilson

7
我曾经遇到过同样的问题。我的解决方案如下:
git remote prune origin 

然后

git fetch --prune

最终只剩下一条活分支,之前有大约20个幽灵分支被清理掉了。

5

谢谢您的回答。然而,那并没有起作用。 - Drew LeSueur

2
git update-ref -d refs/remotes/origin/my_funny_branch

如果那样不行的话,我会在你的.git目录中寻找'my_funny_branch'(可能在.git/refs/remotes/origin中),看看文件权限是否有问题。

1
你试过这个吗?
git push origin :my_funny_branch

是的,我尝试过了。谢谢你的发布。 - Drew LeSueur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接