从 Git 中删除旧的远程分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17470691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:29:58  来源:igfitidea点击:

Remove old remote branches from Git

git

提问by Alex

When I use bash autocompletion in Git, it keeps showing me branches of old remotes that I don't have anymore. When I do a git branch -lait shows those old remotes and branches while a git branch -lwon't. A ls .git/refs/remotes/also shows them. However, they are not present in my .git/configand neither are they shown when I run git remote show.

当我在 Git 中使用 bash 自动完成功能时,它不断向我显示我不再拥有的旧遥控器的分支。当我执行 a 时,git branch -la它会显示那些旧的遥控器和分支,而 agit branch -l则不会。Als .git/refs/remotes/也显示了它们。但是,它们不存在于我的.git/config 中,并且在我运行.git/config 时也不显示它们git remote show

So how do I get rid of them because my autocomplete list is too long right now.

那么我如何摆脱它们,因为我的自动完成列表现在太长了。

I have already tried:

我已经尝试过:

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

I'm also aware of the fact that I can just re-clone the repo but that's just cheating ;-)

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

回答by sleske

Git does not delete the (local) remote-tracking branches automatically if the branch was deleted in the remote repository. Additionally, before V2.0.1 remote-tracking branches were in some cases not deleted when you removed the remote from your git config (see VonC's answer).

如果分支在远程存储库中被删除,Git 不会自动删除(本地)远程跟踪分支。此外,在 V2.0.1 之前,当您从 git 配置中删除远程时,在某些情况下不会删除远程跟踪分支(请参阅 VonC 的回答)。

To delete stale remote-tracking branches (branches that were deleted in the remote repository) for one of your remote repositories, run

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

git remote prune <remote>

To cite the man page or git remote:

引用手册页或git remote

prune

Deletes all stale tracking branches under <name>. These stale branches have already been removed from the remote repository referenced by <name>, but are still locally available in "remotes/<name>".

With --dry-run option, report what branches will be pruned, but do not actually prune them.

修剪

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

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

However, from your question it seems you manually removed .git/refs/remotes/theoldremote, so Git no longer knows about the remote repository that the remote-tracking branches belonged to. That's not how you're supposed to do it.

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

The normal way to remove a remote repository is to run

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

git remote rm <remote>

This will remove the remote from your .git/config, and will delete the remote-tracking branches.

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

If you just delete the directory under .git/refs/remotes/, the branches will remain behind. Then you will need to remove them manually:

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

git branch -rd <remote>/<branchname>

You need option -rto delete a remote branch.

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

回答by Mert

I use

我用

git push origin :remote_branch

to remove a branch from server.

从服务器中删除一个分支。

git remote prune origin

to remove remote references which do not exist on server anymore

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

回答by VonC

Note: while git remote prune is the answer, know that, starting with git 2.0.1 (June 25th, 2014), a git remote rmstarts by removing the remote tracking branches.
So hopefully, one shouldn't have to cleanup old branches after a git remote rm.

注意:虽然 git remote prune 是答案,但要知道,从 git 2.0.1(2014 年 6 月 25 日)git remote rm开始首先删除远程跟踪分支
因此,希望人们不必在git remote rm.

See commit b07bdd3by Jens Lindstr?m (jensl)

提交b07bdd3延Lindstr?米(jensl

remote rm: delete remote configuration as the last

remote rm: 删除远程配置为最后

When removing a remote, delete the remote-tracking branches before deleting the remote configuration.
This way, if the operation fails or is aborted while deleting the remote-tracking branches, the command can be rerun to complete the operation.

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



But if you have to, a simple git fetchcan be enough, provided you have set first:

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

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

回答by aragaer

Push nothingto a branch to delete it:

什么都不推送到一个分支来删除它:

git push remote :remote_branch

git push remote :remote_branch

It's somewhere in the docs but it isn't really obvious.

它在文档中的某个地方,但并不是很明显。

Or did I misunderstand your question?

还是我误解了你的问题?

回答by Alex

Ok I've got it. The problem was that the remotes don't exist anymore, but they do somewhere in the git database. I re-added the remotes, then did

好的,我知道了。问题是遥控器不再存在,但它们在 git 数据库中的某个地方。我重新添加了遥控器,然后做了

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

After that they disappear from the list. Somehow I didn't remove them correctly before I guess.

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

回答by htaccess

I was getting confused when remote branches that had been deleted on the server side were still appearing when I ran:

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

$ git branch --all

The following command fixed this for me (on git version 2.25.0):

以下命令为我解决了这个问题(在 git 版本 2.25.0 上):

$ git remote update --prune