我可以使用 git branch --list --contains
列出
包含某个提交的所有分支就可以了。但正如相关的有关如何列出所有分支的问题中所解释的,这是一个瓷器命令,不应在脚本中使用。
后一个问题建议使用管道命令
git for-each-ref
,但这不支持--contains
。
列出包含特定提交的所有分支的正确管道接口是什么。
18个月后(2017年4月)更新:使用Git 2.13(2017年第2季度),
git for-each-ref --no-contains <SHA1>
终于得到支持了!
参见 提交 7505769、提交 783b829、提交 ac3f5a3、提交 1e0c3b6、提交 6a33814、提交 c485b24、提交 eab98ee ,提交 bf74804(2017 年 3 月 24 日),提交 7ac04f1 、提交 682b29f、提交 4612edc、提交 b643827(2017 年 3 月 23 日)、提交 17d6c74、提交 8881d35、提交 b084060、 提交 0488792(2017 年 3 月 21 日),作者:Ævar Arnfjörð Bjarmason (
avar
).gitster
-- 合并于 commit d1d3d46,2017 年 4 月 11 日)
原答案
从 git 2.7(2015 年第 4 季度)开始,您将获得更完整的
git for-each-ref
版本,现在支持 --contains
git for-each-ref --contains <SHA1>
与医生:
--contains [<object>]:
仅列出包含指定提交的标签(如果未指定则为 HEAD)。
参见 提交 4a71109、提交 ee2bd06、提交 f266c91、提交 9d306b5、提交 7c32834、提交 35257aa、提交 5afcb90 ,...,提交 b2172fd(2015 年 7 月 7 日) ,并提交 af83baf(2015 年 7 月 9 日),作者:Karthik Nayak (
KarthikNayak
)。gitster
-- 合并于 commit 9958dd8,2015 年 10 月 5 日)
“
”和“git tag -l
”的一些功能已经完成 可供“git branch -l
”使用,以便最终统一 后续实施可以在所有三个之间共享 系列或两个。git for-each-ref
* kn/for-each-tag-branch:
for-each-ref: add '--contains' option
ref-filter: implement '--contains' option
parse-options.h: add macros for '--contains' option
parse-option: rename parse_opt_with_commit()
for-each-ref: add '--merged' and '--no-merged' options
ref-filter: implement '--merged' and '--no-merged' options
ref-filter: add parse_opt_merge_filter()
for-each-ref: add '--points-at' option
ref-filter: implement '--points-at' option
使用管道命令
git-for-each-ref
和 git merge-base
(后者由 Joachim 本人建议)的一种可能的解决方案:
#!/bin/sh
# git-branchesthatcontain.sh
#
# List the local branches that contain a specific revision
#
# Usage: git branchthatcontain <rev>
#
# To make a Git alias called 'branchesthatcontain' out of this script,
# put the latter on your search path, and run
#
# git config --global alias.branchesthatcontain \
# '!sh git-branchesthatcontain.sh'
if [ $# -ne 1 ]; then
printf "%s\n\n" "usage: git branchesthatcontain <rev>"
exit 1
fi
rev=$1
git for-each-ref --format='%(refname:short)' refs/heads | \
while read ref; do
if git merge-base --is-ancestor "$rev" "$ref"; then
printf "%s\n" "$ref"
fi;
done
exit $?
该脚本可在 GitHub 上的 jub0bs/git-aliases 获取。
eval
。)