24

How to get latest tag name (like version) of current branch?

VeroLom
  • 3,856
  • 9
  • 34
  • 48
  • 3
    It is not necessary duplicate. I maybe in feature branch, but still like to know what is latest branch of this repo, which may be in master branch. – Ding-Yi Chen Oct 27 '16 at 02:52

1 Answers1

70

git describe should be enough

The command finds the most recent tag that is reachable from a commit.
If the tag points to the commit, then only the tag is shown.
Otherwise, it suffixes the tag name with the number of additional commits on top of the tagged object and the abbreviated object name of the most recent commit.

With --abbrev set to 0, the command can be used to find the closest tagname without any suffix:

[torvalds@g5 git]$ git describe --abbrev=0 v1.0.5^2
tags/v1.0.0

For tags matching a certain pattern:

git describe --tags --abbrev=0 --match release-*

(Peterino's comment)

For the latest tag on all the branches (not just the latest branch)

git describe --tags $(git rev-list --tags --max-count=1)

(from kilianc's answer)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 3
    The latest tag (any, not only annotated) matching a certain pattern (e.g. "release-..."): git describe --tags --abbrev=0 --match release-* – Peterino Jan 28 '13 at 09:39
  • is it possible to get last remote tag? – gayavat Oct 22 '13 at 10:13
  • 2
    @gayavat no, local tag only, but all yu have to do is fetch the remote tags, and if one of those remote tags is applied on a recent commit part of the branch you are describing, then that tag will be part of the describe output. – VonC Oct 22 '13 at 10:17
  • http://stackoverflow.com/a/7979255/350580 provides a more accurate solution. While `git describe --abbrev=0` only tell you the current branch. That answer tell you the latest tag for all branches. – Ding-Yi Chen Oct 27 '16 at 03:16