Get last git tag from a remote repo without cloning

TL;DR

With git ls-remote you can get a list of references from a remote repository.

To see what the latest version is, look at the last line of output from:

git -c 'versionsort.suffix=-' ls-remote --tags --sort='v:refname' <repository>

To only output the latest tag (for instance in a shell script) of a repository that uses Semantic Versioning use:

git -c 'versionsort.suffix=-' \
    ls-remote --exit-code --refs --sort='version:refname' --tags <repository> '*.*.*' \
    | tail --lines=1 \
    | cut --delimiter='/' --fields=3

For older versions of Git that don't have the --sort flag (pre v2.18), or versions that don't support versionsort.suffix (pre v2.4) use:

git ls-remote --refs --tags <repository> \
    | cut --delimiter='/' --fields=3     \
    | tr '-' '~'                         \
    | sort --version-sort                \
    | tail --lines=1

Older versions of sort that don't have the --version-sort flag are out of scope for this question...


The long version

Tags only

Using --tags makes sure the list only contains tag references.

This will include both referenced and de-referenced tags. That means some tags will have ^{} at the end of the refname. (For more information about that see this question elsewhere on StackOverflow.)

For human consumption this doesn't matter much, but if you don't want to see those ^{}'s add --refs.

Sorting

It is possible sort the list of references using --sort.

The sort option uses the same sort keys as git for-each-ref. As we don't have all of the information locally, not all of the options are available to us (for instance date related sort keys).

We want to use version sort, based on the reference name. To do so, we use the version:refname key. This can also be abbreviated to v:refname.

This will sort the versions ascending, meaning the latest version will be last.

To reverse the list prepend the sort key with -: --sort='-v:refname'.

Pre-release sorting

At this point, version-sort will place release candidates (for instance v2.28.0-rc2) after the stable version that they should come in front of.

Since v2.12 we can use a configuration flag that tells Git to sort refnames with a specific character suffix after references without that character suffix: git -c 'versionsort.suffix=-'.

To always use versionsort.suffix like this, it can be set globally:

git config --global 'versionsort.suffix' '-'

Between v2.4 and v2.12 the flag is called versionsort.prereleaseSuffix.

Sorting in older versions of Git

For older Git versions a trick can be used: a dash character - is sorted before a space but a tilde ~ is sorted after a space.

So by replacing the dash - with a tilde ~, things get sorted in the right order. This can be done using tr '-' '~'

One line only

As we don't really care for all of the output, other than the last line, we only show the tail: tail --lines=1. Of course, if the list is retrieved in descending order (with --sort='-v:refname'), this would be: head --lines=1.

Just the Refname

The output from the ls-remote command also outputs the reference hash:

ada126bd28d66c8c8ff5966a52d63ce2c9e4d031        refs/tags/v2.28.0-rc0

To only see the actual tag (i.e. the reference name), we can cut of the first part of the line: cut --delimiter='/' --fields=3

Reference filter

The last thing to note is that ls-remote can be given a filter to only show reference that match the filter pattern. For instance, for Semantic Versioning we could use: '*.*.*'. Anything that does not match that pattern will not be shown.

If the repository always prefixes a version tag with a v, it could be narrowed down further to 'v*.*.*'.

Another example is to only retrieve the latest tag for a specific main version. For instance, to only see tags for verion 2 of a repo, we could use 'v2.*'.

Make sure to use quotes around the filter, otherwise that star * will cause you trouble!

Reference not found

When using a filter it is a good idea to use the --exit-code flag.

This is because Git will always exit with status code 0 to indicate it successfully talked with the remote repository.

For human consumption this is fine, as you'll see on the screen if any refs have been found.

If this code is used in a shell script, however, that can be problematic.

Git can be told to use status code 2 when no matching refs are found in the remote repository. This is done by using the --exit-code flag.

That way a script will know when something goes wrong!

Obviosuly, if no filter is used, using --exit-code does not really make sense.

Time for an example!

Lets say we wanted to know what the latest tag of Git is.

We would do:

git ls-remote --sort='version:refname' --tags https://github.com/git/git.git

That would return a long list with all the tags in order, as shown below (truncated for sanity's sake).

    ...

4c8bcdda4d6e4757caf876ddc401b5392e874e21        refs/tags/v2.28.0
ada126bd28d66c8c8ff5966a52d63ce2c9e4d031        refs/tags/v2.28.0-rc0
bd42bbe1a46c0fe486fc33e82969275e27e4dc19        refs/tags/v2.28.0-rc0^{}
49bfe36405d1631a303992cac5cc408980a0454e        refs/tags/v2.28.0-rc1
3ddac3d691c3633cd4d9a74c07e3b2301f546f77        refs/tags/v2.28.0-rc1^{}
84a0d5cc2107b83a791aa4034cc54874e1d50668        refs/tags/v2.28.0-rc2
b066807397fd55553f4910ede74839e319b661fd        refs/tags/v2.28.0-rc2^{}
47ae905ffb98cc4d4fd90083da6bc8dab55d9ecc        refs/tags/v2.28.0^{}

This tells us the latest tag is v2.28.0.

Another example would be to set versionsort.suffix globally and then get just the last tag:

git config --global 'versionsort.suffix' '-'

git ls-remote --refs --sort=':refname' --tags https://github.com/git/git.git \
    | tail --lines=1 | cut --delimiter='/' --fields=3

Now, let's see if there is already a version 3 of Git!

$ git ls-remote --exit-code --refs --tags https://github.com/git/git.git 'v3.*'
$ echo $?
2 # nope, not yet

Since version 2.18 git has a built-in --sort option for the exact purpose of sorting ref names.

So the up-to-date command would be

git ls-remote --tags --sort="v:refname" git://github.com/git/git.git | tail -n1

To also remove the hash and the dereference marker (^{}), just throw in some simple sed

git ls-remote --tags --sort="v:refname" git://github.com/git/git.git | tail -n1 | sed 's/.*\///; s/\^{}//'

As per suggestion by @Frederik Nord, you can also use the --refs switch to get rid of the ^{}, which leaves just one sed command (making the oneliner 4 characters shorter):

git ls-remote --tags --refs --sort="v:refname" git://github.com/git/git.git | tail -n1 | sed 's/.*\///'

# output: v2.18.0

For git versions prior to 2.18 here's a combo for piping output through sort

git ls-remote --tags git://github.com/git/git.git | sort -t '/' -k 3 -V | awk -F/ '{ print $3 }' | awk '!/\^\{\}/' | tail -n 1

Unfortuntely, git ls-remote --tags actually lists tags alphabetically (at least as of 1.7.2.5). So, at the time that 1.7.10, 1.7.11 or 1.7.12 were the latest tags, 1.7.9 would have been the last on the list:

git ls-remote --tags git://github.com/git/git.git |grep "1\.7\."

[...]
bf68fe0313c833fa62755176f6e24988ef7cf80f        refs/tags/v1.7.9.6
cb2ed324fc917db0b79d7b1f3756575ffa5f70d5        refs/tags/v1.7.9.6^{}
3996bb24c84013ec9ce9fa0980ce61f9ef97be4d        refs/tags/v1.7.9.7
d0f1ea6003d97e63110fa7d50bb07f546a909b6e        refs/tags/v1.7.9.7^{}

However, we can pipe these results through 'sort' to get closer to the results we're looking for:

git ls-remote --tags git://github.com/git/git.git |grep "1\.7\."| sort -g -k3 -t.

[...]
eab05abaeb51531e11835aaa4c26564a1babebac        refs/tags/v1.7.9-rc2
eac2d83247ea0a265d923518c26873bb12c33778        refs/tags/v1.7.9-rc0^{}
f59f511e26b4924b22c6966e79fe4f754bc81dc6        refs/tags/v1.7.9.2
0e2d57fd50f61e668be3180bc8f25991ea88aa8c        refs/tags/v1.7.10-rc1^{}
121f71f0da1bc9a4e1e96be2c3e683191a82a354        refs/tags/v1.7.10.4^{}
26e5c5d09334d157bd04f794f16f6e338d50c752        refs/tags/v1.7.10.3^{}
[...]
cffb45719f60d6fc2cc98ead6af88a895c63c9ac        refs/tags/v1.7.12.4
d8cf053dacb4f78920c112d10c7be21e4f5a5817        refs/tags/v1.7.12.2^{}
dcd07fb6262fd8bb9f531890df3986a8b719a0b5        refs/tags/v1.7.12-rc0
e15c16de396a1e1f42001b03cb885ce64eb4098e        refs/tags/v1.7.12-rc2^{}

While still not correct, it's closer. If we exclude -rc and ^{}, and add an additional sort on the last sub-version number, we can probably get close enough for most needs:

git ls-remote --tags git://github.com/git/git.git |grep "1\.7\."|grep -v -|grep -v {| sort -n -t. -k3 -k4

23ed9debf17263ed6bed478a4d6d86e71342c18a        refs/tags/v1.7.11.6
527b331100ddba839cc54bb31c1bcd66acc08321        refs/tags/v1.7.11.7
14d20a75e3d57a872a8c81ae90dcc4c61ddba011        refs/tags/v1.7.12
51993a414a76120fda20d56ba767fa513d9ff440        refs/tags/v1.7.12.1
04043f4d1ae42bddee67d354a2e6fd2464592a1e        refs/tags/v1.7.12.2
b38da673be332933b8f3a873ce46ffea08d2ee2c        refs/tags/v1.7.12.3
cffb45719f60d6fc2cc98ead6af88a895c63c9ac        refs/tags/v1.7.12.4