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?
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?
To get the most recent tag:
To get the most recent annotated tag:
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.
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
.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.
Get the last tag with prefix "ac". For example, tag named with
ac1.0.0
, orac1.0.5
. Other tags named1.0.0
,1.1.0
will be ignored.Get the last tag, whose first char is
0-9
. So, those tags with first chara-z
will be ignored.More info
Update
With
git tag --help
, about thesort
argument. It will uselexicorgraphic order
by default, iftag.sort
property doesn't exist.After google, someone said git 2.8.0 support following syntax.
My first thought is you could use
git rev-list HEAD
, which lists all the revs in reverse chronological order, in combination withgit tag --contains
. When you find a ref wheregit tag --contains
produces a nonempty list, you have found the most recent tag(s).To get the most recent tag, you can do:
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.