是否有可能在git for-each-ref中过滤掉合并的分支? [英] Is it possible to filter out merged branches in git for-each-ref?
问题描述
基本上我想要类似于git branch -a --no-merged
的东西,但是我也想列出标签,并且我不需要空格或remotes/origin/HEAD -> origin/master
Basically I want something similar to git branch -a --no-merged
but I'd also like to list tags and I don't need the spaces before or stuff like remotes/origin/HEAD -> origin/master
我可以使用for-each-ref进行此操作吗?还是最好只从git branch
中剥离不需要的内容,然后分别获取标签?
Can I do this with for-each-ref or is it better to just strip the stuff I don't want from git branch
then get the tags separately?
推荐答案
git for-each-ref
和类似的git show-ref
不能自行决定一个引用是否是另一个引用的祖先,但您始终可以构建管道,例如:
git for-each-ref
and the similar git show-ref
do not have the ability to decide whether one reference is an ancestor of another on its own, but you can always build a pipeline, e.g.:
uninteresting=master # or some other way to identify ones to discard
git for-each-ref |
while read sha1 reftype refname; do
if git merge-base --is-ancestor $refname $uninteresting; then
# $refname / $sha1 "is merged", or is exactly equal
... do whatever you like here ...
else
# there is no ancestry path from $uninteresting back to $refname
... do whatever else you like here ...
fi
done
请注意,git merge-base --is-ancestor master master
始终为true.如果您不希望将等于但名称不同"视为合并"(并且这取决于它是分支还是其他引用类型?),您将在此处需要一些额外的代码,但是我怀疑您确实希望等于表示合并".
Note that git merge-base --is-ancestor master master
is always true. If you don't want "equal but name differs" considered to be "merged" (and does this depend on whether it's a branch or some other ref type?) you will need some extra code here, but I suspect you do want "equal means merged".
这篇关于是否有可能在git for-each-ref中过滤掉合并的分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!