git fetch origin --prune 不删除本地分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37664226/
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 fetch origin --prune doesn't delete local branches?
提问by yano
At one point I thought that git fetch origin --prune
deleted local branches that were no longer present on the server. Somehow this is not my experience at the moment.
一度我认为git fetch origin --prune
删除了服务器上不再存在的本地分支。不知何故,这不是我目前的经验。
I ran this command, and the local branch was not deleted. It is notcurrently checked out. I ran git branch -vv
to check this info, and I see
我运行了这个命令,本地分支没有被删除。目前尚未签出。我跑去git branch -vv
检查这个信息,我看到
feature/MyGreatFeature f30efc7 [origin/feature/MyGreatFeature: gone]
So it seems to know that it is gone. Why would it not delete my local branch?
所以它似乎知道它已经过去了。为什么它不会删除我的本地分支?
Running git version 2.7.4 (Apple Git-66)
跑步 git version 2.7.4 (Apple Git-66)
回答by torek
The various prune options (git remote update --prune
, git remote prune
, git fetch --prune
) onlydelete remote-tracking branches.1
各种修剪选项 ( git remote update --prune
, git remote prune
, git fetch --prune
)仅删除远程跟踪分支。1
You'll need to manually delete local branches you no longer want, or change or remove their upstream setting if the remote-tracking branch no longer exists. Note that each local branch can record a remote and/or branch that do not now, or even never did, exist. In this case Git mostly acts as if those local branches have no upstream set, except that since version 1.8.5, several commands report the upstream as "gone" or otherwise invalid, and may suggest using --unset-upstream
.
您需要手动删除不再需要的本地分支,或者如果远程跟踪分支不再存在,则更改或删除其上游设置。请注意,每个本地分支都可以记录现在不存在,甚至从未存在过的远程和/或分支。在这种情况下,Git 主要表现为那些本地分支没有上游设置,除了自 1.8.5 版以来,一些命令将上游报告为“消失”或以其他方式无效,并可能建议使用--unset-upstream
.
1More precisely, they delete destination refs after doing the refspec mapping from the command line or fetch
lines from the configuration. Hence, for fetch mirrors, they candelete local branches. Most clones are not set up as fetch mirrors, though.
1更准确地说,他们在从命令行或fetch
配置行执行 refspec 映射后删除目标引用。因此,对于获取镜像,他们可以删除本地分支。不过,大多数克隆并未设置为获取镜像。
There were some recent bug fixes for complex mappings, to make sure that Git did not prune a mapped branch in some cases when it should not. For any normal repository—ordinary clone or pure fetch mirror—these fixes have no effect; they matter only if you have complicated fetch
configurations.
最近有一些针对复杂映射的错误修复,以确保 Git 在某些情况下不会在不应该修剪映射分支的情况下进行修剪。对于任何普通的存储库——普通克隆或纯获取镜像——这些修复都不起作用;只有当您有复杂的fetch
配置时,它们才重要。
回答by Jonathan Holmes
The command you want is
你想要的命令是
$ git remote prune origin
This questionis almost word for word what you're looking for.
这个问题几乎是一字不差的。
回答by Brent Worden
The following command chain can be used to delete local branches:
以下命令链可用于删除本地分支:
git branch --v | grep "\[gone\]" | awk '{print }' | xargs git branch -D
- git branch --vlists the local branches verbosely
- grep "[gone]"finds all the branches whose remote branch is gone
- awk '{print $1}'outputs only the name of the matching local branches
- xargs git branch -Ddeletes all the matching local branches
- git branch --v详细列出本地分支
- grep "[gone]"查找远程分支消失的所有分支
- awk '{print $1}'只输出匹配的本地分支的名称
- xargs git branch -D删除所有匹配的本地分支
This should work on MacOS as well as *nix environments.
这应该适用于 MacOS 以及 *nix 环境。