How to get tags on current commit

Viewed 49956

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?

8 Answers

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)

This is not ideal, but perhaps helpful:

$ git log -n 1 --decorate --pretty=oneline

You could play around with the format to get exactly what you want.

I'm doing git tag -l | grep $(git describe HEAD) it returns the tag of the latest commit, or nothing if the last commit isn't tagged

Related