fl00r fl00r - 2 months ago 15
Git Question

Get last git tag from a remote repo without cloning

How to get last tag from a (non checked-out) remote repo?

On my local copy I use

describe


git describe --abbrev=0 --tags


But I cannot use
describe
with remote storage

Answer

Use git ls-remote --tags <repository>

For example, if we want to know what the latest tag that Git is at we would do

git ls-remote --tags git://github.com/git/git.git

That returns a long list with all the tags in alphabetical order, as shown below (truncated for sanity's sake). The last line tells us the latest tag is v1.8.0-rc0.

Keep in mind that tags can be any kind of string so, as pointed out by Christopher Gervais in his answer, git ls-remote sorts tags alphabetically. Unfortunately git ls-remote does not have a --sort option (like, for example git for-each-ref), so your best option is to use native sort.

More recent versions of sort support the -V or --version-sort flag to do a natural sort of (version) numbers within text.

So to sort them naturally, your command would look like this:

git ls-remote --tags git://github.com/git/git.git | sort -t '/' -k 3 -V

Please take a look at (and upvote) Christopher Gervais's answer below if you need more/other greping options.


     ...
 e4dc716b1cfefb0e1bd46c699d4f74009118d001   refs/tags/v1.7.9
 828ea97de486c1693d6e4f2c7347acb50235a85d   refs/tags/v1.7.9^{}
 cc34c0417dfd4e647e41f3d34a032b7164aadea7   refs/tags/v1.7.9-rc0
 eac2d83247ea0a265d923518c26873bb12c33778   refs/tags/v1.7.9-rc0^{}
 ad2ec9a47a031ebf056444a94bea3750aaa68f63   refs/tags/v1.7.9-rc1
 6db5c6e43dccb380ca6e9947777985eb11248c31   refs/tags/v1.7.9-rc1^{}
 eab05abaeb51531e11835aaa4c26564a1babebac   refs/tags/v1.7.9-rc2
 bddcefc6380bd6629f3f12b5ffd856ec436c6abd   refs/tags/v1.7.9-rc2^{}
    ...
 5ace0b7af106b44687005085d8c252f8be9da5d3   refs/tags/v1.8.0-rc0
 b0ec16b49eb283156e13bbef26466d948e4fd992   refs/tags/v1.8.0-rc0^{}
Comments