How can I get the latest tag name in current branch in Git?

347,750

Solution 1

You could take a look at git describe, which does something close to what you're asking.

Solution 2

To get the most recent tag (example output afterwards):

git describe --tags --abbrev=0   # 0.1.0-dev

To get the most recent tag, with the number of additional commits on top of the tagged object & more:

git describe --tags              # 0.1.0-dev-93-g1416689

To get the most recent annotated tag:

git describe --abbrev=0

Solution 3

Will output the tag of the latest tagged commit across all branches

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

Solution 4

To get the most recent tag, you can do:

$ git for-each-ref refs/tags --sort=-taggerdate --format='%(refname)' --count=1

Of course, you can change the count argument or the sort field as desired. It appears that you may have meant to ask a slightly different question, but this does answer the question as I interpret it.

Solution 5

How about this?

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

Technically, won't necessarily get you the latest tag, but the latest commit which is tagged, which may or may not be the thing you're looking for.

Share:
347,750
culebrón
Author by

culebrón

My GitHub profile, my CV at careers. Erde: hiking-light geospatial toolkit I'm a Python & Javascript programmer, know some DBs well. Know some Linux stuff. I speak Russian, Italian, Spanish and English fluently. I'm fond of cycling, MTB orienteering, travelling.

Updated on July 08, 2022

Comments

  • culebrón
    culebrón almost 2 years

    What's the simplest way to get the most recent tag in Git?

    git tag a HEAD
    git tag b HEAD^^
    git tag c HEAD^
    git tag
    

    output:

    a
    b
    c
    

    Should I write a script to get each tag's datetime and compare them?