1

I can achieve the effect I want by running:

git log -1 --decorate --oneline <commit hash>

and reading the tag names and branch names.

Is there a way to get this information directly?

Essentially what I want is the inverse operation of:

git rev-parse <tag or branch>^{commit}
Wildcard
  • 1,302
  • 2
  • 20
  • 42
  • @Code-Apprentice, that is a very different question, not a duplicate. The answers there are all information I was aware of when I wrote this question. – Wildcard Feb 17 '16 at 00:46
  • 1
    I realized that after looking a little more closely. I rescinded my close vote but didn't delete the automatically generated comment. My apologize for the confusion. – Code-Apprentice Feb 17 '16 at 00:48

3 Answers3

4

Git 2.7.0 introduced a --points-at flag to various subcommands:

git for-each-ref --points-at <commit> --format '%(refname)'
git branch --points-at <commit>
git tag --points-at <commit>
MForster
  • 8,806
  • 5
  • 28
  • 32
3
git branch --contains <commit>
git tag --contains <commit>

Edit: To find the refs that point exactly to hash abc123, do:

git show-ref | awk '/^abc123/ {print $2}'
David Deutsch
  • 17,443
  • 4
  • 47
  • 54
2

If you don't have git 2.7+ you can still use git for-each-ref to find refs that point at specific commits, by filtering the for-each-ref output:

git for-each-ref | grep ^1234567

for instance. [Edit: this is essentially the same as using git show-ref, except that following tags to their target requires more typing. The for-each-ref output is meant for scripts, though, so if you want to write a script, it's probably better to use this than to use git show-ref.]

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775