从Git删除旧的远程分支


80

当我在Git中使用bash自动补全功能时,它会不断向我显示我不再拥有的旧遥控器的分支。当我这样做时,git branch -la它会显示那些旧的遥控器和分支机构,而git branch -l不会。Als .git/refs/remotes/也显示了它们。但是,它们不在我的.git / config中,在我运行时也不会显示git remote show

那么,由于我的自动填充列表太长了,我该如何删除它们。

我已经尝试过:

git reflog expire --expire=now --all
git gc --prune=now
rm .git/refs/remotes/theoldremote
git remote prune theoldremote

我也知道我可以重新克隆仓库,但这只是作弊;-)


注意:git remote rm现在(git 2.0.1,2014年6月)首先删除远程跟踪分支。那应该有助于避免清理旧的分支。请在下面
VonC 2014年


这是我的另一个答案:stackoverflow.com/a/44129766/3835843
Arif

Answers:


141

如果在远程存储库中删除了分支,Git不会自动删除(本地)远程跟踪分支。另外,在某些情况下,当从git config中删除遥控器时,在某些情况下不会删除V2.0.1之前的远程跟踪分支(请参见VonC的答案)。

要删除其中一个远程存储库的陈旧的远程跟踪分支(在远程存储库中删除的分支),请运行

git remote prune <remote>

引用手册页或git remote

修剪

删除<名称>下的所有过时的跟踪分支。这些过时的分支已从<name>引用的远程存储库中删除,但仍可在本地使用“ remotes / <name>”。

使用--dry-run选项,报告将修剪哪些分支,但实际上不修剪它们。

但是,从您的问题看来,您似乎是手动删除的.git/refs/remotes/theoldremote,因此Git不再了解远程跟踪分支所属的远程存储库。那不是你应该做的。

删除远程存储库的正常方法是运行

git remote rm <remote>

这将从您的中.git/config删除遥控器,并将删除远程跟踪分支。

如果您只删除目录下的目录.git/refs/remotes/,则分支将保留在后面。然后,您将需要手动删除它们:

git branch -rd <remote>/<branchname>

您需要选择-r删除远程分支。


2
不,不是这样。它说fatal: 'kolichikov' does not appear to be a git repository
亚历克斯

2
有关在获取/拉出时自动修剪远程分支的信息,另请参见:stackoverflow.com/a/18718936/968201
Patrick James McDougle 2014年

如果发现此速度较慢,请升级至已修复的新Git(2.0.1+ IIRC)(速度提高100倍以上)。
奥迪尼奥-Velmont 2014年

修剪用户注意事项:Git文档说:“大多数情况下,用户应运行git gc,这将调用git prune。 ”。但是请注意与git gc不兼容--dry-run
JYL

git branch -rd <remote>/<branchname>是我一直在寻找的东西。谢谢!
安东尼·科尔森

16

我用

git push origin :remote_branch

从服务器删除分支。

git remote prune origin

删除服务器上不再存在的远程引用


不,不是这样。它说fatal: 'kolichikov' does not appear to be a git repository
亚历克斯

1
Alex,您必须在主目录中而不是主目录中运行命令……
SparK

6

注意:虽然git remote prune是答案,但请注意,从git 2.0.1(2014年6月25日)git remote rm 开始,a会删除远程跟踪分支
因此,希望人们不必在结束后清理旧分支git remote rm

参见JensLindströmcommit b07bdd3 jensl

remote rm:最后删除远程配置

删除远程服务器时,请先删除远程跟踪分支,然后再删除远程配置。
这样,如果在删除远程跟踪分支时操作失败或中止操作,则可以重新运行命令以完成操作


但是如果必须的话git fetch,只要先设置一个简单的就足够了:

git config --global fetch.prune true
cd /path/to/repo
git config remote.origin.prune true


4

向分支推送任何内容以将其删除:

git push remote :remote_branch

它在文档中的某处,但并不十分明显。

还是我误会了你的问题?


2
“它在文档中的某处,但并不十分明显。” 这是git
mnagel

“远程”是指您的远程存储库名称。例如“起源”之类的。但是我开始认为您的意思不只是删除远程分支。
aragaer


4

好的,我知道了。问题在于远程控制器已不复存在,但它们在git数据库中的某个位置存在。我重新添加了遥控器,然后做了

git remote prune theremote
git remote rm theremote
git gc --prune=now

之后,它们从列表中消失。我不知何故没有正确删除它们。


我从大量来源尝试了所有方法,唯一可行的方法是重新远程添加问题,然后将其删除。谢谢!
jc00ke '17

0

当我运行时仍在服务器端删除的远程分支仍然出现时,我感到困惑:

$ git branch --all

以下命令为我修复了此问题(在git版本2.25.0上):

$ git remote update --prune
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.