git remote prune – 没有像我预期的那样显示尽可能多的修剪分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4040717/
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
git remote prune – didn't show as many pruned branches as I expected
提问by Felixyz
From the man page:
从手册页:
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>".
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>".
So I removed a bunch of branches using
所以我删除了一堆分支使用
git push origin :staleStuff
and then ran
然后跑了
git remote prune origin
However, only one single local branch was pruned. Some of these branches were created by me, some by co-workers. Does this indicate that I wasn't tracking those branches correctly in the first place?
但是,只有一个本地分支被修剪。这些分支有些是我创建的,有些是同事创建的。这是否表明我一开始没有正确跟踪这些分支?
回答by max
When you use git push origin :staleStuff
, it automatically removes origin/staleStuff
, so when you ran git remote prune origin
, you have pruned some branch that was removed by someone else. It's more likely that your co-workers now need to run git prune
to get rid of branches you have removed.
当您使用 时git push origin :staleStuff
,它会自动删除origin/staleStuff
,因此当您运行时git remote prune origin
,您已经修剪了一些被其他人删除的分支。您的同事现在更有可能需要运行git prune
以摆脱您已删除的分支。
So what exactly git remote prune
does? Main idea: local branches (not tracking branches) are not touched by git remote prune
command and should be removed manually.
那么具体有什么git remote prune
作用呢?主要思想:本地分支(不是跟踪分支)不受git remote prune
命令影响,应该手动删除。
Now, a real-world example for better understanding:
现在,一个真实世界的例子可以更好地理解:
You have a remote repository with 2 branches: master
and feature
. Let's assume that you are working on both branches, so as a result you have these references in your local repository (full reference names are given to avoid any confusion):
您有一个带有 2 个分支的远程存储库:master
和feature
. 假设您同时在两个分支上工作,因此您的本地存储库中有这些引用(给出了完整的引用名称以避免任何混淆):
refs/heads/master
(short namemaster
)refs/heads/feature
(short namefeature
)refs/remotes/origin/master
(short nameorigin/master
)refs/remotes/origin/feature
(short nameorigin/feature
)
refs/heads/master
(简称master
)refs/heads/feature
(简称feature
)refs/remotes/origin/master
(简称origin/master
)refs/remotes/origin/feature
(简称origin/feature
)
Now, a typical scenario:
现在,一个典型的场景:
- Some other developer finishes all work on the
feature
, merges it intomaster
and removesfeature
branch from remote repository. - By default, when you do
git fetch
(orgit pull
), no references are removed from your local repository, so you still have all those 4 references. - You decide to clean them up, and run
git remote prune origin
. - git detects that
feature
branch no longer exists, sorefs/remotes/origin/feature
is a stalebranch which should be removed. - Now you have 3 references, including
refs/heads/feature
, becausegit remote prune
does not remove anyrefs/heads/*
references.
- 其他一些开发人员完成了对 的所有工作
feature
,将其合并到远程存储库中master
并feature
从远程存储库中删除了分支。 - 默认情况下,当您执行
git fetch
(或git pull
) 时,不会从本地存储库中删除任何引用,因此您仍然拥有所有这 4 个引用。 - 您决定清理它们,然后运行
git remote prune origin
. - git 检测到该
feature
分支不再存在,因此应该删除refs/remotes/origin/feature
一个陈旧的分支。 - 现在您有 3 个引用,包括
refs/heads/feature
, 因为git remote prune
不会删除任何refs/heads/*
引用。
It is possible to identify local branches, associated with remote tracking branches, by branch.<branch_name>.merge
configuration parameter. This parameter is not really required for anything to work (probably except git pull
), so it might be missing.
可以通过branch.<branch_name>.merge
配置参数识别与远程跟踪分支相关联的本地分支。此参数对于任何工作都不是真正必需的(可能除了git pull
),因此它可能会丢失。
(updated with example & useful info from comments)
(更新了评论中的示例和有用信息)