How to list all tags pointing to a specific commit in git

GitTags

Git Problem Overview


I have seen the commands git describe and git-name-rev but I have not managed to get them to list more than one tag.

Example: I have the sha1 48eb354 and I know the tags A and B point to it. So I want a git command git {something} 48eb354 that produce output similar to "A, B". I am not interested in knowing references relative other tags or branches just exact matches for tags.

Git Solutions


Solution 1 - Git

git tag --points-at HEAD

Shows all tags at HEAD, you can also substitute HEAD with any sha1 id.

Solution 2 - Git

You can use:

git tag --contains <commit>

that shows all tags at certain commit. It can be used instead of:

git tag --points-at HEAD

that is available only from 1.7.10.

Solution 3 - Git

git show-ref --tags -d | grep ^48eb354 | sed -e 's,.* refs/tags/,,' -e 's/\^{}//'

should work for both lightweight and annotated tags.

Solution 4 - Git

http://www.kernel.org/pub/software/scm/git/docs/git-for-each-ref.html">git for-each-ref --format='%(objectname) %(refname:short)' refs/tags/ |
grep ^$commit_id |
cut -d' ' -f2

Pity it can’t be done more easily. Another flag on git tag to include commit IDs could express that git for-each-ref invocation naturally.

Solution 5 - Git

For current commit you can use

git tag --points-at $(git log -n1 --pretty='%H')

Solution 6 - Git

The following command does the job, but directly parse the content of the .git directory and thus may break if the git repository format change.

grep -l -r -e '^48eb354' .git/refs/tags|sed -e 's,.*/,,'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZitraxView Question on Stackoverflow
Solution 1 - Gituser2159398View Answer on Stackoverflow
Solution 2 - GityorammiView Answer on Stackoverflow
Solution 3 - GitmaxView Answer on Stackoverflow
Solution 4 - GitAristotle PagaltzisView Answer on Stackoverflow
Solution 5 - GitProgmanView Answer on Stackoverflow
Solution 6 - GitSylvain DefresneView Answer on Stackoverflow