How to get tags on current commit

I have a repository which has multiple tags on the same commit. For example:

commit #3 <--- TAG1 / TAG2 / TAG3

  |

commit #2 <--- TAG4/ TAG5

  |

commit #1 <--- TAG6/ TAG7

I'd like to find out what tags are on a particular commit. For example, if I check commit 1, I'd like to get tag 6 and tag 7.

I have tried:

git checkout <commit 1> 
git tag --contains

which displayed tags 1-7.

git checkout <commit 1>
git describe --tags HEAD

displayed tag 6 only.

What is the proper way to do this in Git?


Solution 1:

For completion (thanks to Ciro Santili answer), git tag has got the option --points-at that does exactly what OP is asking.

git tag --points-at HEAD

It does not have the effect of also listing the tags put on forward commits (as Jonathan Hartley stated in his comment regarding git tag --contains).

Solution 2:

I guess maybe git has had some options added since this question was asked, but since it still comes in pretty high on google, I thought I'd add that this way works nicely:

git tag -l --contains HEAD

Or replace HEAD with any other valid commit reference you like.

This will print a newline separated list of tags if the HEAD contains any tags, and print nothing otherwise, so you would get:

TAG6
TAG7

And of course there are lots of nice ways with various other shell tools that you can format that output once you have it...

Solution 3:

Some improvements on William's answer:

git config --global alias.tags 'log -n1 --pretty=format:%h%d'

The output looks like this:

~$ git tags
7e5eb8f (HEAD, origin/next, origin/master, origin/HEAD, master)
~$ git tags HEAD~6
e923eae (tag: v1.7.0)

Solution 4:

git tag --points-at

--points-at

Only list tags of the given object (HEAD if not specified). Implies --list.

from https://git-scm.com/docs/git-tag