Git scripting: list of all git branches containing commit

I can list all branches containing a specific commit using git branch --list --contains just fine. But, as explained in a related question about how to list all branches , this is a china command that should not be used in scripts.

The final question is to use the git for-each-ref plumbing command, but this does not support --contains .

What is the correct plumbing interface to list all branches containing a specific commit.

+6
source share
2 answers

One possible solution using the git-for-each-ref and git merge-base plumbing commands (the latter was suggested by Joachim himself):

 #!/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 $? 

The script is available in Jubobs / git-aliases on GitHub.

(Edit: thanks coredump for showing me how to get rid of this nasty eval .)

+2
source

Update after 18 months (April 2017) : with Git 2.13 (Q2 2017), git for-each-ref --no-contains <SHA1> is finally supported!

See commit 7505769 , commit 783b829 , commit ac3f5a3 , commit 1e0c3b6 , commit 6a33814 , commit c485b24 , commit eab98ee , commit bf74804 (March 24, 2017), commit 7ac04f1 , commit 682b29f , commit 4612edc , commit b643827 (March 23, 2017 ) and commit 17d6c74 , commit 8881d35 , commit b084060 , commit 0488792 (March 21, 2017) Γ†var ArnfjΓΆrΓ° Bjarmason ( avar ) .
(Merger of Junio ​​With Hamano - gitster - on commit d1d3d46 , April 11, 2017)


Original answer

Running Git 2.7 (Q4 2015) will give you a more complete version of git for-each-ref , which now support --contains

 git for-each-ref --contains <SHA1> 

With document

 --contains [<object>]: 

Only list tags that contain the specified commit (HEAD if not specified).


See commit 4a71109 , commit ee2bd06 , commit f266c91 , commit 9d306b5 , commit 7c32834 , commit 35257aa , commit 5afcb90 , ..., commit b2172fd (July 07, 2015) and commit af83baf (July 9, 2015) Karthik Nayak ( KarthikNayak )
(merger of Junio ​​C Hamano - gitster - on commit 9958dd8 , October 05, 2015)

Some functions from " git tag -l " and " git branch -l " were made available for " git for-each-ref " so that the implementation could eventually be distributed among all three, in a subsequent series or two.

 * 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 
+5
source

All Articles