108

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

248

git tag --points-at HEAD

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

1
62

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.

3
  • 18
    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.
    – staafl
    May 10, 2016 at 9:54
  • 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. May 19, 2016 at 20:42
  • --contains also appears much slower than --points-at (7-8s execution time vs < 1s)
    – SSilk
    Mar 15, 2018 at 14:09
50

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

should work for both lightweight and annotated tags.

4
  • Thanks. Exactly what i want.
    – mrutyunjay
    Dec 27, 2013 at 10:02
  • This works well for git < 1.7.10. Can you explain what that second part of the sed expression does? -e 's/\^{}//'
    – Dave
    Feb 28, 2016 at 20:56
  • 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."
    – Dave
    Feb 28, 2016 at 21:14
  • 1
    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' Mar 9, 2016 at 22:11
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
  • 1
    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.
    – Zitrax
    Dec 28, 2010 at 13:47
  • 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!
    – darthn
    Sep 15, 2022 at 9:58
3

For current commit you can use

git tag --points-at $(git log -n1 --pretty='%H')
1
  • 3
    Just use HEAD instead of $(git log -n1 --pretty='%H')
    – qoomon
    Feb 28, 2021 at 14:02
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
  • 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.
    – Zitrax
    Dec 28, 2010 at 14:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.