114

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?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
wes
  • 1,643
  • 2
  • 15
  • 12

6 Answers6

141

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>
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • 46
    `git describe --tags ` for unannotated tags – Aleksandr Levchuk Sep 24 '11 at 14:21
  • 2
    I love this. Spefically, I love that I can get a human readable from the current revision: `git describe --all --exact-match \`git rev-parse HEAD\`` – dsummersl Feb 19 '13 at 18:40
  • 6
    In newer versions, `git describe --tags --abbrev=0 REV` will be useful when you don't want the junk on the tag. – Craig Ringer Dec 13 '13 at 07:47
  • 1
    `git describe --all` (for the currently checked out commit) or `git describe --all ` have always done just about everything I ever wanted. – rfay Apr 26 '14 at 17:59
  • The best part of using versions from git is no more hardcoding of the version in source when a software is deployed. The git describe's way of finding the nearest tag can be used to calculate the next version number and just add a tag ref to the repo and the package name. – Sid Jul 07 '16 at 19:35
  • 2
    This does not give all the tags associated to the commit if there's more than one! IMO the `git tag --points-at ` answer below is probably what most people are looking for. – Louis Dionne Sep 28 '22 at 13:51
80

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.

Robin Green
  • 32,079
  • 16
  • 104
  • 187
Jay
  • 801
  • 6
  • 2
  • This is what most people arriving here probably want, and what you would expect based on the title of the question - even though reading the question carefully, it seems not quite what the OP wanted. – Andrew Spencer Apr 16 '21 at 09:13
53

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).

29

You can find this information in the manual

git tag --contains <commit>
Brian Webster
  • 30,033
  • 48
  • 152
  • 225
dharga
  • 2,187
  • 3
  • 24
  • 33
  • 4
    This will list all of the tags which contain the given commit (i.e. tags which point to children of the given commit), so it's not necessarily going to give a single tag for a given commit or any information about which tag is closest, unless the list only contains a single commit. – CB Bailey Sep 24 '09 at 21:11
  • 5
    As Charles Bailey says above, that's not exactly what I'm looking for. Maybe you should RTFM :) – wes Sep 24 '09 at 21:28
  • 1
    Well, sorry for the wrong answer. When I checked it seemed to do what you were asking for. I did RTFM, I was just solving the wrong problem. Hope Bailey offered a solution that helps you out. – dharga Sep 25 '09 at 03:29
  • 7
    you shouldn't need to read the whole git manual before you ask a git question on SO, right? google indexes SO much better than any git man page... – aaronstacy Oct 17 '12 at 14:04
  • Lol, this is exactly what I want by searching for my original (different from the OP's) problem. Why looking at a problem not the same with mine? Google led me here. – Escape0707 Apr 16 '23 at 03:27
17

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
bschlueter
  • 3,817
  • 1
  • 30
  • 48
  • 1
    `git describe --tags --abbrev=0 HEAD` returns the tag and doesn't require stripping for CI. – Kosh Feb 03 '21 at 22:57
4

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

bschlueter
  • 3,817
  • 1
  • 30
  • 48