How to find the tag associated with a given git commit?

Git

Git Problem Overview


For releases I usually tag with something like v1.1.0. During my build script I am creating a fwVersion.c file that contains the current git info. Currently, I have commit, and branch info in the file, but I would like to add the tag.

Is this possible?

Git Solutions


Solution 1 - Git

Check the documentation for git describe. It finds the nearest tag to a given commit (that is a tag which points to an ancestor of the commit) and describes that commit in terms of the tag.

If you only want to know if the commit is pointed to by a tag then you can check the output of:

git describe --exact-match <commit-id>

Solution 2 - Git

If what you want is the first tag containing the commit then:

git describe --contains <commit>

gives the best answer IMO. If you have frequent tags than using "git tag --contains" on an old commit in a big repository can take a while to run and gives you all of the tags that contain that commit.

This form of git describe runs very quickly and gives you a single output value which is the first tag containing the commit and how far back your commit is.

Solution 3 - Git

How about this?

git tag --points-at <commit-id>

It gives you all the tags the given commit has (whereas git describe only gives you one), and does not include tags on descendant commits (like git tag --contains does).

Solution 4 - Git

You can find this information in the manual

git tag --contains <commit>

Solution 5 - Git

I found the combo of both top answers to give me what i wanted like so:

git describe --tags --exact-match <commit-id>

This gives you the tag that is ONLY for that commit and for ones without annotation. Useful when you want to find tags and not worry about stripping the formatting off then (for Jenkins for example).

eg. $ git describe --tags --exact-match head~2

Gives you:

$ ReleaseBeta

Solution 6 - Git

Consolidating some of the answers:

git tag --contains [<ref>]

and

git tag --points-at [<ref>]

or just

git tag

behave the same, printing any (and all) tags for the specified ref or the current commit if not specified.

git describe --tags [<ref>]

where <ref> defaults to the current commit, exits with 128 if no tags are associated with the commit, and prints a tag associated with the commit (there does not seem to be a pattern).

git describe [<ref>] behaves the same as with --tags except that it only prints annotated tags.

Supplying the option --contains to describe will print the a tag which is associated with an ancestor of the specified commit. For example

$ git init
Initialized empty Git repository in /tmp/test
$ git commit -m one --allow-empty
[master (root-commit) 7fdfff2] one
$ git commit -m two --allow-empty
[master cd5f8f1] two
$ git tag -am foo foo
$ git tag bar
$ git log --format=oneline
cd5f8f1f4f29eb164f83e224768ccaf37fe170ed (HEAD -> master, tag: foo, tag: bar) two
7fdfff2ce5e3347f8eee4c9f2413dbd4e90060e1 one
$ git describe 7fdfff2ce5e3347f8eee4c9f2413dbd4e90060e1
fatal: No tags can describe '7fdfff2ce5e3347f8eee4c9f2413dbd4e90060e1'.
Try --always, or create some tags.
$ git describe --contains 7fdfff2ce5e3347f8eee4c9f2413dbd4e90060e1
bar~1

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
QuestionwesView Question on Stackoverflow
Solution 1 - GitCB BaileyView Answer on Stackoverflow
Solution 2 - GitJayView Answer on Stackoverflow
Solution 3 - GitAlbertas AgejevasView Answer on Stackoverflow
Solution 4 - GitdhargaView Answer on Stackoverflow
Solution 5 - GitChristopher AlexanderView Answer on Stackoverflow
Solution 6 - GitbschlueterView Answer on Stackoverflow