2

Is there a Git command (or series of commands) which, given a commit SHA, lists all branches currently at that commit?

For example, given the following state:

* 040cc7c     (HEAD, master)
| 
| * a9a246b   (develop, feature_a)
| * 73b91cc   
| * 001b20a   (feature_b, feature_c)
|/
* d642f88

Issuing the hypothetical command git <something> 001b20a would output feature_b, feature_c. Note that I don't want to see develop or feature_a in the output; therefore I don't think git branch --contains 001b20a is the correct command, at least not in its vanilla form.

Matt Dunn
  • 5,106
  • 6
  • 31
  • 55
  • 1
    As a sidenote, `git log -1 --pretty=format:"%d"` will list branches but also tags (**not required here**) pointing at said commit (by showing decorations). – Romain Valeri Jun 01 '21 at 14:11
  • 2
    I think this is a great example of posting a question and answer together when you research something and find the answer, and want to help the community. This is a well written question and nice answer. Unfortunately in this particular case though, it is a dup: [See what git branches (or tags) point to given commit hash?](https://stackoverflow.com/questions/35444831/see-what-git-branches-or-tags-point-to-given-commit-hash) – TTT Jun 01 '21 at 15:11

1 Answers1

4

The command to achieve this behaviour is:

git branch --points-at <commit SHA>

This can be modified using the --remotes flag to only show remote branches, or --all to show both remote and local-tracking branches.

Matt Dunn
  • 5,106
  • 6
  • 31
  • 55