681

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?

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
culebrón
  • 34,265
  • 20
  • 72
  • 110
  • 4
    latest created tag or latest tag ordered by commit date? Your accepted answer shows the latest created tag. This could be a problem if one decides to alter existig tags ... – Paebbels Apr 17 '16 at 18:44

25 Answers25

940

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
Per Lundberg
  • 3,837
  • 1
  • 36
  • 46
acassis
  • 9,569
  • 1
  • 15
  • 4
  • 5
    Yep, as the man page for `git describe` says: `--abbrev= [...] An of 0 will suppress long format, only showing the closest tag.` – Giorgos Kylafas Mar 28 '12 at 13:19
  • 10
    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
  • 4
    if I have more than two tag, then this doesn't print them all – Sungguk Lim Feb 03 '15 at 00:41
  • This is what I was looking for. I wanted a way to see the most recent "version" tag without having to manually look through the list myself. When a git repository uses tags to let you download the most recent stable version, this does the job of getting the most recent "version" tag for that project. – JohnRDOrazio Aug 26 '15 at 09:05
  • 9
    if you want to directly checkout the most recent tag: `git checkout $(git describe --abbrev=0 --tags)` – JohnRDOrazio Aug 26 '15 at 09:13
  • Add `--always` to always create a tag (SHA hash if there is none), otherwise if there is no tag it would spill error messages and exist status 128. – hakre May 06 '16 at 07:32
  • 2
    Old thread, and feeling stupid, but `git fetch --tags origin && git describe --abbrev=0 --tags` does not return the latest tag pulled down by the `fetch` operation. Why not, I wonder? – Laird Nelson Sep 21 '17 at 22:57
  • 5
    This answer is better than current "accepted", because it gives exact command line to use, not just "look at the documentation" – Ezh Mar 05 '19 at 11:23
  • 2
    `--abbrev=0` is needed in both commands to display only the tag name (without subsequent commit count and hash). And to clarify, `--tags` searches for lightweight tags in addition to annotated tags by default. – fbastien Jan 26 '20 at 22:38
  • If "you wish to not match tags on branches merged in the history of the target commit" use `git describe --tags --abbrev=0 --first-parent` – user1507435 Nov 24 '20 at 15:26
  • And to find first tag for specific branch, just append the branch name, like `git describe --tags upstream/v5.0.0` – Hi-Angel Oct 15 '21 at 11:34
  • This solution is not working on a repository without any tag. – Hossein Mayboudi Aug 08 '23 at 05:00
507

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

git describe --tags $(git rev-list --tags --max-count=1)
kilianc
  • 7,397
  • 3
  • 26
  • 37
  • 29
    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
  • 5
    @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
  • 8
    Notes: This command returns the "newest" tag even this tag is on another branch. – Alan Zhiliang Feng Jul 16 '15 at 10:17
  • 4
    I disagree with the claim that the question is about a particular branch. The title of the question implies that, but the question that is actually asked is "What's the simplest way to get the most recent tag in Git?" I guess it's a meta question if the title is the question or not. – William Pursell May 31 '18 at 00:12
  • Regardless of what the OP was asking for, bless you for this answer. It's exactly what I was looking for, latest tag name across all branches without the hash. Thanks @kilianc ! –  May 07 '19 at 20:01
  • Even though the question asks for the current branch this helped me in finding the latest tag in the best possible way and Thanks for that! – Panduka DeSilva Mar 12 '20 at 06:26
  • 1
    I believe nobody wants to get the latest tag of current branch. Because tag is unique across all branches, getting tag of only a branch just doesn't make sense or help anything at all. This awnser should be marked as accepted answer. – emeraldhieu Sep 03 '20 at 09:28
  • 1
    If you only want to get the latest tag on main, you can use: `git describe --tags $(git rev-list --branches=main --tags --max-count=1)` – seg Dec 01 '20 at 10:45
  • This is the answer exactly I was looking for and thank you so much for posting! – Saravanan C Jan 11 '23 at 17:02
504

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

JB.
  • 40,344
  • 12
  • 79
  • 106
61

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.

the_drow
  • 18,571
  • 25
  • 126
  • 193
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • 3
    This is almost exactly what I was looking for (the most recent tag across all branches), but at least with my version of git (1.7.7.6) the tags are produced in the same order for both `--sort=-authordate` and `--sort=authordate`. – larsks Aug 01 '12 at 14:37
  • 4
    Aha, because what you actually want is `--sort=-taggerdate`. For tags, `authordate` and `committerdate` are empty (so useless as sort keys). – larsks Aug 01 '12 at 14:41
  • 13
    `git for-each-ref refs/tags --sort=-taggerdate --format='%(refname:short)' --count=1` is even better :) – Christophe Eblé Nov 12 '15 at 18:02
  • 1
    Please note that it returns the "newest" tag in terms of the tagged time, may not be the most "recent" tag along the tree. For instance, you can checkout any previous commit and tag that commit. The "newest" tag would be the newly created tag. – Randy Lai Dec 18 '15 at 20:01
  • This should work even for shallow clones. `--format='%(tag)` will give you a simple tag too. – Ryan Jan 18 '18 at 01:03
  • 1
    And `--points-at=$SHA` will give you the tag for a commit hash. – Ryan Jan 18 '18 at 01:03
  • This is not for a branch. This is the latest tag in the repo period. – Chaim Eliyah May 30 '18 at 22:35
  • I think you responded elsewhere... still had the window open. Definitely not saying that you didn't answer the actual question. The Googles will take you here if you're looking for how to get the tag for a branch, and there are SO redirects as well. – Chaim Eliyah May 31 '18 at 02:28
  • To confine this to just the current branch, add `--merged`. – jthill Nov 17 '20 at 18:23
  • In my case (git version 2.34.0), `--sort=-authordate` is actually what worked... only our "annotated" tag had a taggerdate. – lmsurprenant Jan 07 '22 at 13:13
  • Note: if you have many types of tags split by `/`, you can include the prefix in the query. As pointed above, `%(refname:short)` is nice as it strips the `refs/tags` prefix in output. I also wanted to know the time of the tag. My final solution: `git for-each-ref refs/tags/build/prod --sort=-taggerdate --format='%(refname:short) -- %(creatordate:relative)' --count=1` – jakub.g Jun 03 '22 at 15:20
47

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.

Zombo
  • 1
  • 62
  • 391
  • 407
  • 1
    This one doesn't get the latest tag in the current branch but for any branches. Which is great, cause it's exactly what I needed. Thanks Wincent. – jasongregori Aug 26 '14 at 19:02
  • 4
    If you need to get tag matching pattern you can use `--tags=` in `rev-list`. For example, get last version tag `git describe --tags $(git rev-list --tags='v[0-9].[0-9]*' --max-count=1)` – Wirone Sep 03 '14 at 12:31
  • 2
    $(git describe --contains $(git rev-parse HEAD)) – Vaidas Zilionis Jan 04 '15 at 23:08
  • Thanks, it worked, but, I had to add --tags `git describe --tags $(git rev-list --tags --max-count=1)` – Gianluca Casati Jun 19 '15 at 12:35
34

You can execute: git describe --tags $(git rev-list --tags --max-count=1) talked here: How to get latest tag name?

xserrat
  • 1,429
  • 1
  • 13
  • 12
  • I started with the top/accepted answer and worked my way down. This is the first answer that actually gives me the latest tag (not sure why but plain old `git describe ...` returns an earlier tag?!) – Jeremy Davis Sep 05 '19 at 06:06
  • This is the only one that works for me. Tried some of the `--abbrev=0` answers and they cut off part of the tag that I want. – Luke Davis Sep 17 '19 at 02:11
34

The problem with describe in CI/CD processes is you can run into the fatal: no tags can describe error.

This will occur because, per git describe --help:

The command finds the most recent tag that is reachable from a commit.

If you want the latest tag in the repo, regardless if the branch you are on can reach the tag, typically because it is not part of the current branch's tree, this command will give you the most recently created tag in the entire repo:

git tag -l --sort=-creatordate | head -n 1

More details and other keys to sort on can be found here: https://git-scm.com/docs/git-for-each-ref#_field_names

Shanerk
  • 5,175
  • 2
  • 40
  • 36
  • This doesn't name seem to work in my case. Using powershell on git version 2.37.2.windows.2. I verified this by changing the sort from: --sort=-creatordate to --sort=creatordate and still got the same result order. – Anders Larsen Nov 28 '22 at 14:11
  • I'm on OSX currently using git 2.15.x and likely an older version when I posted this. – Shanerk Nov 30 '22 at 00:43
  • Not sure what `creatordate` means, but `committerdate` works fine for me too: `git tag -l --sort=-committerdate | head -n1`. – pawamoy Jul 01 '23 at 10:11
  • For commit and tag objects, the special creatordate and creator fields will correspond to the appropriate date or name-email-date tuple from the committer or tagger fields depending on the object type. These are intended for working on a mix of annotated and lightweight tags. https://git-scm.com/docs/git-for-each-ref#_field_names – Shanerk Jul 02 '23 at 11:31
31
git describe --abbrev=0 --tags

If you don't see latest tag, make sure of fetching origin before running that:

git remote update
nik7
  • 806
  • 3
  • 12
  • 20
Walter B
  • 655
  • 6
  • 8
  • 1
    Thank you, simply doing `git describe --abbrev=0` as suggested in other answers return v1.0.0 which is not latest local tag, adding `--tags` however gives the right tag. – Herz3h Jul 21 '21 at 06:16
30

I'm not sure why there are no answers to what the question is asking for. i.e. All tags (non-annotated included) and without the suffix:

git describe --tags --abbrev=0
smac89
  • 39,374
  • 15
  • 132
  • 179
pooya13
  • 2,060
  • 2
  • 23
  • 29
28

"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:

https://github.com/MacPython/terryfy/blob/master/git-closest-tag

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.

Matthew Brett
  • 863
  • 8
  • 11
27
git tag --sort=committerdate | tail -1
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
user2957741
  • 993
  • 2
  • 11
  • 11
25

git describe --tags

returns the last tag able to be seen by current branch

james_womack
  • 10,028
  • 6
  • 55
  • 74
beenhere4hours
  • 970
  • 9
  • 12
19
git tag -l ac* | tail -n1

Get the last tag with prefix "ac". For example, tag named with ac1.0.0, or ac1.0.5. Other tags named 1.0.0, 1.1.0 will be ignored.

git tag -l [0-9].* | tail -n1

Get the last tag, whose first char is 0-9. So, those tags with first char a-z will be ignored.

More info

git tag --help # Help for `git tag`

git tag -l <pattern>

List tags with names that match the given pattern (or all if no pattern is given). Running "git tag" without arguments also lists all tags. The pattern is a shell wildcard (i.e., matched using fnmatch(3)). Multiple patterns may be given; if any of them matches, the tag is shown.


tail -n <number> # display the last part of a file
tail -n1 # Display the last item 

Update

With git tag --help, about the sort argument. It will use lexicorgraphic order by default, if tag.sort property doesn't exist.

Sort order defaults to the value configured for the tag.sort variable if it exists, or lexicographic order otherwise. See git-config(1).

After google, someone said git 2.8.0 support following syntax.

git tag --sort=committerdate
Community
  • 1
  • 1
AechoLiu
  • 17,522
  • 9
  • 100
  • 118
17

What is wrong with all suggestions (except Matthew Brett explanation, up to date of this answer post)?

Just run any command supplied by other on jQuery Git history when you at different point of history and check result with visual tagging history representation (I did that is why you see this post):

$ git log --graph --all --decorate --oneline --simplify-by-decoration

Todays many project perform releases (and so tagging) in separate branch from mainline.

There are strong reason for this. Just look to any well established JS/CSS projects. For user conventions they carry binary/minified release files in DVCS. Naturally as project maintainer you don't want to garbage your mainline diff history with useless binary blobs and perform commit of build artifacts out of mainline.

Because Git uses DAG and not linear history - it is hard to define distance metric so we can say - oh that rev is most nearest to my HEAD!

I start my own journey in (look inside, I didn't copy fancy proof images to this long post):

What is nearest tag in the past with respect to branching in Git?

Currently I have 4 reasonable definition of distance between tag and revision with decreasing of usefulness:

  • length of shortest path from HEAD to merge base with tag
  • date of merge base between HEAD and tag
  • number of revs that reachable from HEAD but not reachable from tag
  • date of tag regardless merge base

I don't know how to calculate length of shortest path.

Script that sort tags according to date of merge base between HEAD and tag:

$ git tag \
     | while read t; do \
         b=`git merge-base HEAD $t`; \
         echo `git log -n 1 $b --format=%ai` $t; \
       done | sort

It usable on most of projects.

Script that sort tags according to number of revs that reachable from HEAD but not reachable from tag:

$ git tag \
    | while read t; do echo `git rev-list --count $t..HEAD` $t; done \
    | sort -n

If your project history have strange dates on commits (because of rebases or another history rewriting or some moron forget to replace BIOS battery or other magics that you do on history) use above script.

For last option (date of tag regardless merge base) to get list of tags sorted by date use:

$ git log --tags --simplify-by-decoration --pretty="format:%ci %d" | sort -r

To get known current revision date use:

$ git log --max-count=1

Note that git describe --tags have usage on its own cases but not for finding human expected nearest tag in project history.

NOTE You can use above recipes on any revision, just replace HEAD with what you want!

Community
  • 1
  • 1
gavenkoa
  • 45,285
  • 19
  • 251
  • 303
16
git log --tags --no-walk --pretty="format:%d" | sed 2q | sed 's/[()]//g' | sed s/,[^,]*$// | sed  's ......  '

IF YOU NEED MORE THAN ONE LAST TAG

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

east
  • 193
  • 1
  • 6
  • 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 03 '14 at 13:27
  • 1
    To take this further: `git log --tags --no-walk --pretty="format:%D" | sed -nr '5q;s;^.*(tag: )([^,]*).*;\2;p'` where in `%D` excludes the surrounding `()` chars, and the `sed`s starting 5q leaves 4 lines before 5, then prints out all characters between 'tag: ` and the first ','. So... assuming no commas are used inside the tag, this works perfectly. – Cometsong Feb 21 '19 at 15:13
5

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.

Ivan Vučica
  • 9,529
  • 9
  • 60
  • 111
5

If you want to find the last tag that was applied on a specific branch you can try the following:

git describe --tag $(git rev-parse --verify refs/remotes/origin/"branch_name")
Scyssion
  • 374
  • 5
  • 7
5

If you need a one liner which gets the latest tag name (by tag date) on the current branch:

git for-each-ref refs/tags --sort=-taggerdate --format=%(refname:short) --count=1 --points-at=HEAD

We use this to set the version number in the setup.

Output example:

v1.0.0

Works on Windows, too.

Markus
  • 1,360
  • 1
  • 16
  • 22
  • 1
    On Linux (Debian Stretch) I get `-bash: syntax error near unexpected token '('` – Jeremy Davis Sep 05 '19 at 06:05
  • 3
    If I wrap that part in quotes (i.e. `--format="%(refname:short)"`) and skip the `--points-at=HEAD` it works. With that last switch it returns nothing, I guess because my `HEAD` isn't tagged? – Jeremy Davis Sep 05 '19 at 06:12
  • @JeremyDavis, yes I think that's because your `HEAD` is not tagged. – Markus Sep 10 '19 at 09:22
4

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

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
2

This is an old thread, but it seems a lot of people are missing the simplest, easiest, and most correct answer to OP's question: to get the latest tag for the current branch, you use git describe HEAD. Done.

Edit: you can also supply any valid refname, even remotes; i.e., git describe origin/master will tell you the latest tag that can be reached from origin/master.

dohpaz42
  • 520
  • 4
  • 18
  • Nothing guarantees that "the most recent tag" is handled by any branch. The two working solutions are the `for-each-ref` command (http://stackoverflow.com/a/5261470/515973) and the combination of `rev-list` and `describe` (http://stackoverflow.com/a/7979255/515973) – Julien Carsique Apr 14 '17 at 14:01
  • 1
    This does not get the most recent tag for me. – basickarl Oct 09 '17 at 13:04
  • `git describe branchname --tags` does work for me to get latest tag on branch (git version 2.12.2) – Rob_M Jan 03 '19 at 14:58
2
git tag --sort=-refname | awk 'match($0, /^[0-9]+\.[0-9]+\.[0-9]+$/)' | head -n 1 

This one gets the latest tag across all branches that matches Semantic Versioning.

emeraldhieu
  • 9,380
  • 19
  • 81
  • 139
  • but only if they have exactly three segments, and also does not handle ordering correctly... `9.9.9` will be returned as later than `10.1.1` – Anentropic Dec 15 '20 at 17:35
1

For the question as asked,

How to get the latest tag name in the current branch

you want

git log --first-parent --pretty=%d | grep -m1 tag:

--first-parent tells git log not to detail any merged histories, --pretty=%d says to show only the decorations i.e. local names for any commits. grep -m1 says "match just one", so you get just the most-recent tag.

jthill
  • 55,082
  • 5
  • 77
  • 137
0

To get the latest tag only on the current branch/tag name that prefixes with current branch, I had to execute the following

BRANCH=`git rev-parse --abbrev-ref HEAD` && git describe --tags --abbrev=0 $BRANCH^ | grep $BRANCH

Branch master:

git checkout master

BRANCH=`git rev-parse --abbrev-ref HEAD` && git describe --tags 
--abbrev=0 $BRANCH^ | grep $BRANCH

master-1448

Branch custom:

git checkout 9.4

BRANCH=`git rev-parse --abbrev-ref HEAD` && git describe --tags 
--abbrev=0 $BRANCH^ | grep $BRANCH

9.4-6

And my final need to increment and get the tag +1 for next tagging.

BRANCH=`git rev-parse --abbrev-ref HEAD` && git describe --tags  --abbrev=0 $BRANCH^ | grep $BRANCH | awk -F- '{print $NF}'
Sankarganesh Eswaran
  • 10,076
  • 3
  • 23
  • 24
0

if your tags are sortable:

git tag --merged $YOUR_BRANCH_NAME | grep "prefix/" | sort | tail -n 1
Clintm
  • 4,505
  • 3
  • 41
  • 54
0

Not much mention of unannotated tags vs annotated ones here. 'describe' works on annotated tags and ignores unannotated ones.

This is ugly but does the job requested and it will not find any tags on other branches (and not on the one specified in the command: master in the example below)

The filtering should prob be optimized (consolidated), but again, this seems to the the job.

git log  --decorate --tags master |grep '^commit'|grep 'tag:.*)$'|awk '{print $NF}'|sed 's/)$//'|head -n 1

Critiques welcome as I am going now to put this to use :)

Rondo
  • 3,458
  • 28
  • 26