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

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

git remote prune – didn't show as many pruned branches as I expected

gitversion-controlbranchgit-branchgit-remote

提问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 pruneto get rid of branches you have removed.

当您使用 时git push origin :staleStuff,它会自动删除origin/staleStuff,因此当您运行时git remote prune origin,您已经修剪了一些被其他人删除的分支。您的同事现在更有可能需要运行git prune以摆脱您已删除的分支。



So what exactly git remote prunedoes? Main idea: local branches (not tracking branches) are not touched by git remote prunecommand 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: masterand 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 个分支的远程存储库:masterfeature. 假设您同时在两个分支上工作,因此您的本地存储库中有这些引用(给出了完整的引用名称以避免任何混淆):

  • refs/heads/master(short name master)
  • refs/heads/feature(short name feature)
  • refs/remotes/origin/master(short name origin/master)
  • refs/remotes/origin/feature(short name origin/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:

现在,一个典型的场景:

  1. Some other developer finishes all work on the feature, merges it into masterand removes featurebranch from remote repository.
  2. By default, when you do git fetch(or git pull), no references are removed from your local repository, so you still have all those 4 references.
  3. You decide to clean them up, and run git remote prune origin.
  4. git detects that featurebranch no longer exists, so refs/remotes/origin/featureis a stalebranch which should be removed.
  5. Now you have 3 references, including refs/heads/feature, because git remote prunedoes not remove any refs/heads/*references.
  1. 其他一些开发人员完成了对 的所有工作feature,将其合并到远程存储库中masterfeature从远程存储库中删除了分支。
  2. 默认情况下,当您执行git fetch(或git pull) 时,不会从本地存储库中删除任何引用,因此您仍然拥有所有这 4 个引用。
  3. 您决定清理它们,然后运行git remote prune origin.
  4. git 检测到该feature分支不再存在,因此应该删除refs/remotes/origin/feature一个陈旧的分支。
  5. 现在您有 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>.mergeconfiguration 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)

(更新了评论中的示例和有用信息)