Commits can always find their own ancestors but not their parents, so once you've found a commit you can't go UP the commit tree to find branch tips that are reachable.
Rather than first finding commits and then finding branches from those, you could ask each branch for its commits that match your issue number, then print those in a friendly manner.
Here's a commit tree that, I think, shows the workflow you're using:
$ git log --graph --decorate --pretty=oneline --abbrev-commit --all
* c332e51 (HEAD -> 1.x-dev) [Issue_4] Fix a problem with bar.
* a5a1c89 Add some copy to foo.
* b2e21e4 [Issue_3] Get baz some copy.
* 62df79a [Issue_2] Tweak bar a bit.
* 0c10193 [Issue_1] Adjust foo.
| * ab3f45f (2.x-dev) [Issue_3] Get baz some copy.
| * db14d19 [Issue_2] Tweak bar a bit.
| * 8722417 [Issue_1] Adjust foo.
| * 091fd82 Tweak baz.
|/
* eb9dace (master) This is bar
* 26d9260 Adjust foo.
* 15cd4ef Added some files.
* d73dbe7 Initial commit
I have two dev branches (1.x-dev
and 2.x-dev
). I've indicated issue-fixing commits with the string [Issue_NN]
at the beginning of the commit message (mainly to make it easy to see from the tree). I've cherry-picked the issue commits from branch to branch. You can see that Issues 1-3 are applied to both branches, but Issue 4 is only on 1.x-dev
.
So now, assuming that matches your practice, you can find the names of all the branches that contain [Issue_1]
by listing all the branches and running your git log
command just on that branch, then munging the output to conditionally display. For example:
# grab branch names from refs/heads
for branch in $(git for-each-ref --format='%(refname:short)' refs/heads/); \
# set a var to the log line of the matching commit, if any
do commit=$(git log --pretty=oneline --abbrev-commit --grep "Issue_3" $branch); \
# list the branch and commit info if it matches
[[ $commit != "" ]] && echo "$branch: $commit"; \
done
This code looks for Issue_3
and if you run it you get output like:
1.x-dev: b2e21e4 [Issue_3] Get baz some copy.
2.x-dev: ab3f45f [Issue_3] Get baz some copy.
If you run it for Issue_4
, however:
1.x-dev: c332e51 [Issue_4] Fix a problem with bar.
you can see that the Issue_4
commit has been applied only to 1.x-dev
.
There may be prettier ways to accomplish this but I think the general principle is that you'll need to ask the branches for their commits, rather than finding commits then working backwards to branches.