114

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.

0

6 Answers 6

259

git tag --points-at HEAD

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

Sign up to request clarification or add additional context in comments.

2 Comments

This works for both lightweight and annotated tags. This should be marked as the correct answer, as it's short, simple, and works. I understand, however, that the accepted answer pre-dates git v1.7.10 where this functionality is added, so cudos to @max for his answer too! Upvoted!
65

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.

4 Comments

NB: 'git tag --contains' doesn't show only the tags that point to the commit, but also includes any later tags that it's reachable from.
At least in version 2.6.4, this only returns a single tag that points to the specified commit. It doesn't not return them all. The answer above by @max seems to work properly for multiple tags.
--contains also appears much slower than --points-at (7-8s execution time vs < 1s)
@SSilk When I ran git tag --points-at HEAD, it was incredibly slow. Then subsequently running git tag --contains HEAD, it ran lightening fast. Given that's the exact opposite of what you experienced, perhaps there's some caching going on that makes "subsequent lookups" faster, but executing either command for the first time will be slow due to a "cache miss".
52

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

should work for both lightweight and annotated tags.

3 Comments

This works well for git < 1.7.10. Can you explain what that second part of the sed expression does? -e 's/\^{}//'
Ah, nevermind, figured it out. It's for the -d option on git show-ref. From the docs, "-d, --dereference Dereference tags into object IDs as well. They will be shown with "^{}" appended."
Slight enhancement/generalization for using arbitrary ${ref}, including annotated tags: hash=$(git rev-parse "${ref}^0"); git show-ref --tags -d | sed -n -e 's,^'"${hash}"' refs/tags/\(.*\)^{}$',\1,p'
6

(Edit: This answer was written long before git tag had the --points-at switch – which is what you would use nowadays.)

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.

2 Comments

Thanks for the info but I seem not to be able to see the tags using this command either. Using the simpler "git for-each-ref | grep 48eb354" gives 0 matches. gitk on the other hand nicely lists the 2 tags in front of this commit.
This solution alone also works perfectly when the .git/objects folder is deleted! The solution i was looking for, to reduce the size of my docker build context. Thanks!
3

For current commit you can use

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

1 Comment

Just use HEAD instead of $(git log -n1 --pretty='%H')
1

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,.*/,,'

1 Comment

Does not list them either, the only place I see the sha1 if grepping the entire .git folder is in gitk.cache and logs/refs/remotes/origin/master. How come gitk can list them ? I also notice that the tags I am looking at are annotated tags, this might be why the two current answers are not working for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.