Take the 2-minute tour ×

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



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

share|improve this question
add comment

10 Answers

up vote 75 down vote accepted

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

share|improve this answer
With --abbrev=0 it should return closest annotated tag –  Jakub Narębski Sep 10 '09 at 12:34
Returns the latest tag in the current branch. –  brittohalloran Dec 21 '11 at 16:32
To get the latest annotated tag which targets only the current commit in the current branch, use git describe --exact-match --abbrev=0. –  Naftuli Tzvi Kay Jul 24 '12 at 15:05
This answer, with comments, did not provide the correct tag for me. @kilianic provided a correct solution. –  Herman J. Radtke III Dec 29 '12 at 1:34
add comment

This one worked:

git describe --abbrev=0 --tags
share|improve this answer
Yep, as the man page for git describe says: --abbrev=<n> [...] An <n> of 0 will suppress long format, only showing the closest tag. –  Giorgos Kylafas Mar 28 '12 at 13:19
Or just git describe --tags –  james_womack May 16 '13 at 17:22
This answer (and probably the others) runs into trouble if you have two tags pointing to the same commit. Unless the tags are annotated tags, I think there is no way for git to distinguish which of the two was created earlier. –  Paul Lynch Jul 31 '13 at 20:36
add comment

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

share|improve this answer
or TAG=$(git describe --tags $(git rev-list --tags --max-count=1)) @william-pursell –  kilianc Nov 2 '11 at 11:40
This is the best answer. It actually gets tags across all branches, not just the current branch, like the current "correct" answer does. –  brittohalloran Dec 21 '11 at 16:27
Except the question specifically asks for the current branch. –  michaeltwofish Oct 24 '12 at 2:12
@michaeltwofish, at the time I didn't know it, I figured it out later and corrected accordingly my answer. Sorry about that. –  kilianc Oct 30 '12 at 14:42
add comment

git describe --tags

returns the last tag able to be seen by current branch

share|improve this answer
Please leave a meaningful comment for down voting. –  dmasi Feb 9 at 2:57
add comment

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.

share|improve this answer
This is almost exactly what I was looking for (the most recent tag across all branches), but at least with my version of git ( the tags are produced in the same order for both --sort=-authordate and --sort=authordate. –  larsks Aug 1 '12 at 14:37
Aha, because what you actually want is --sort=-taggerdate. For tags, authordate and committerdate are empty (so useless as sort keys). –  larsks Aug 1 '12 at 14:41
@larsks Edited :) –  the_drow Jun 18 '13 at 20:57
add comment

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|improve this answer
add comment
git log --tags --no-walk --pretty="format:%d" | sed 2q | sed 's/[()]//g' | sed s/,[^,]*$// | sed  's ......  '


(git describe --tags sometimes gives wrong hashes, i dont know why, but for me --max-count 2 doesnt work)

this is how you can get list with latest 2 tag names in reverse chronological order, works perfectly on git 1.8.4. For earlier versions of git(like 1.7.*), there is no "tag: " string in output - just delete last sed call

If you want more than 2 latest tags - change this "sed 2q" to "sed 5q" or whatever you need

Then you can easily parse every tag name to variable or so.

share|improve this answer
this is really helpful, many developers will try to automate release process by rolling back to previous tag if deployment breaks. Awesome stuff!!! –  AkD Jul 3 at 13:27
add comment

My first thought is you could use git rev-list HEAD, which lists all the revs in reverse chronological order, in combination with git tag --contains. When you find a ref where git tag --contains produces a nonempty list, you have found the most recent tag(s).

share|improve this answer
add comment

The following works for me in case you need last two tags (for example, in order to generate change log between current tag and the previous tag). I've tested it only in situation where the latest tag was the HEAD.

PreviousAndCurrentGitTag=`git describe --tags \`git rev-list --tags --abbrev=0 --max-count=2\` --abbrev=0`
PreviousGitTag=`echo $PreviousAndCurrentGitTag | cut -f 2 -d ' '`
CurrentGitTag=`echo $PreviousAndCurrentGitTag | cut -f 1 -d ' '`

GitLog=`git log ${PreviousGitTag}..${CurrentGitTag} --pretty=oneline | sed "s_.\{41\}\(.*\)_; \1_"`

It suits my needs, but as I'm no git wizard, I'm sure it could be further improved. I also suspect it will break in case the commit history moves forward. I'm just sharing in case it helps someone.

share|improve this answer
add comment

"Most recent" could have two meanings in terms of git.

You could mean, "which tag has the creation date latest in time", and most of the answers here are for that question. In terms of your question, you would want to return tag c.

Or you could mean "which tag is the closest in development history to some named branch", usually the branch you are on, HEAD. In your question, this would return tag a.

These might be different of course:

A->B->C->D->E->F (HEAD)
       \     \
        \     X->Y->Z (v0.2)
         P->Q (v0.1)

Imagine the developer tag'ed Z as v0.2 on Monday, and then tag'ed Q as v0.1 on Tuesday. v0.1 is the more recent, but v0.2 is closer in development history to HEAD, in the sense that the path it is on starts at a point closer to HEAD.

I think you usually want this second answer, closer in development history. You can find that out by using git log v0.2..HEAD etc for each tag. This gives you the number of commits on HEAD since the path ending at v0.2 diverged from the path followed by HEAD.

Here's a Python script that does that by iterating through all the tags running this check, and then printing out the tag with fewest commits on HEAD since the tag path diverged:


git describe does something slightly different, in that it tracks back from (e.g.) HEAD to find the first tag that is on a path back in the history from HEAD. In git terms, git describe looks for tags that are "reachable" from HEAD. It will therefore not find tags like v0.2 that are not on the path back from HEAD, but a path that diverged from there.

share|improve this answer
add comment

Your Answer


