通过管道命令的Git分支状态 [英] Git Branch Status via Plumbing Command
问题描述
是否可以通过管道命令获取git branch -v
的输出?确切地说,我只对分支的状态感兴趣,即是否为[gone]
.
Is there any way to get the output of git branch -v
as a plumbing command? To be exact I'm only interested in the state of the branch, i.e. whether it is [gone]
or not.
例如,给出以下git branch -v
输出:
> git branch -v
master 32c59ad4 Some other comment
someDeletedBranch 6aacba47 [gone] Some Comment
我如何获得someDeletedBranch
裁判?
请注意,这与git branch --merged
是不相同,例如,如果您要将拉取请求压缩到主数据库中,那么
Note that this is not the same as git branch --merged
, if for example you're squashing pull requests into your master, so this solution won't do.
这主要与此问题有关,因为这将是缺少能够创建可靠脚本以删除远程不再存在的本地分支的部分.
This is mostly in relation to this question, since this would be the missing part for being able to create a reliable script to remove local branches that don't exist on the remote any longer.
推荐答案
仅Git v2.13.2 +
以Mark的答案为基础,并这篇文章,这是一个非常干净的解决方案(为便于阅读,使用了分割线):
Git v2.13.2+ only
Building off of Mark's answer, and this post, here's a pretty clean solution (split lines for readability):
git for-each-ref \
--format='%(if:equals=[gone])%(upstream:track)%(then)%(refname:short)%(end)' \
refs/heads
好处:
- 仅使用管道命令,请参阅
-
"gone" as plumbing message
-
upstream:track
显示[gone]
- only uses plumbing commands, see
"gone" as plumbing message
upstream:track
displaying[gone]
- 解析和比较逻辑由
--format
- 应该在支持git的所有平台上工作
使用此命令的简单方法是使用
git branch -D
进行命令替换.请注意,如果没有要删除的分支, git将会抱怨.A simple way to use this is with command substitution with
git branch -D
. Note that git will complain if there are no branches to delete.git branch -D $(git for-each-ref --format='%(if:equals=[gone])%(upstream:track)%(then)%(refname:short)%(end)' refs/heads)
这里是一个稍长的版本,如果没有要删除的分支,git不会抱怨.
Here is a slightly longer version where git doesn't complain if there are no branches to delete.
for branch in $(git for-each-ref --format='%(if:equals=[gone])%(upstream:track)%(then)%(refname:short)%(end)' refs/heads); do git branch -D $branch; done
或者,您可以将
git for-each-ref
的输出保存到变量中,然后有条件地将分支(如果不为空)删除,或者将其显示为消息.Alternatively, you can save the output of
git for-each-ref
into a variable, then conditionally delete the branch if it's not empty, or show a message if it is.这篇关于通过管道命令的Git分支状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
Benefits:
-