How to get list of latest tags in remote git?

34

10

There are alot of methods to get latest tags when you have local git repo.

But i want to get list of latest tags on remote repo.

I know about "git ls-remote", and everything is fine when you use tags like x.y.z (where x,y,z are numbers). But when tags looks like "test-x.y.z" and "dev-x.y.z" i noticed that large amount of "test" tags will pull out any new "dev" tags, which is not correct.

So, how would you like solve this?

Psychozoic

Posted 2013-12-22T21:21:36.190

Reputation: 426

Are you saying that having many "test" tags causes "dev" tags to not be included in the output of git ls-remote? I find that hard to believe. What if you specify a tag pattern to limit the output to only the desired tags (git ls-remote ... dev-*)? – Magnus Bäck – 2013-12-22T22:09:35.643

i'm saying, that if i need only 10 last tags - i will not see any new "dev" tags if there will be 10 or more "test" tags. – Psychozoic – 2013-12-23T10:32:40.733

i'm trying to make groovy script work as i need. Groovy script i saw there: http://stackoverflow.com/questions/7157170/can-i-get-jenkins-to-build-a-git-tag-from-a-passed-in-parameter

– Psychozoic – 2013-12-23T10:35:37.280

Note: With Git 2.18 (Q2 2018), you now have git ls-remote --sort: see my answer below

– VonC – 2018-05-20T23:12:54.857

Answers

34

Do you use linux? If so you can use this command

git ls-remote --tags | grep -o 'refs/tags/dev-[0-9]*\.[0-9]*\.[0-9]*' | sort -r | head | grep -o '[^\/]*$'

It will show you 10 latest tags (with name dev-x.y.z)

UPD
You can use this bash script to get latest tags:

#!/bin/bash

TAGS=("dev-[0-9]*\.[0-9]*\.[0-9]*" "test-[0-9]*\.[0-9]*\.[0-9]*" "good-[0-9]*" "new [0-9][0-9][0-9]")

for index in ${!TAGS[*]}
do
    git ls-remote --tags | grep -o "refs/tags/${TAGS[$index]}" | sort -rV | head | grep -o '[^\/]*$'
done

Just add in array TAGS regular expressions that you want, and you'll get 10 latest tags for every of them. If you want to get more or less tags, just add param -n to head command 'head -n 5' or 'head -n 15'.

Just in case. Save it in folder ~/bin (for example with name git_tags), then add executable permission (chmod +x git_tags), this will allow you to run this bash script from every place (just type git_tags).

cooperok

Posted 2013-12-22T21:21:36.190

Reputation: 3 271

but it will sort only "dev" tags. but, thanks... i'll think in that way – Psychozoic – 2013-12-23T13:30:57.180

Do you need to get both tags (dev and test)? – cooperok – 2013-12-23T14:36:33.583

yes! and actually it would be grete to use any other tags, like: new-xxx, good-dd.mm.dd, dd.mm.dd. or any else.

but if it is not possible - so ok. – Psychozoic – 2013-12-23T21:09:59.747

I wrote small bash script, you can use it to get tags what you want. Also if you'll want to be able to get tags only for one type (just dev or just test) I can write you script which will do it – cooperok – 2013-12-24T08:08:03.840

that nice, thanks! – Psychozoic – 2013-12-24T08:21:28.667

that sucks, that i can't vote for your solution :( – Psychozoic – 2013-12-24T08:26:18.740

8

some guy told me that command:

git ls-remote -t repo.url.git | awk '{print $2}' | cut -d '/' -f 3 | cut -d '^' -f 1  | sort -b -t . -k 1,1nr -k 2,2nr -k 3,3r -k 4,4r -k 5,5r | uniq

and this is not the best solution, but he opened my eyes on command sort.

but i would like to know other versions.

Psychozoic

Posted 2013-12-22T21:21:36.190

Reputation: 426

2it incorrectly orders 0.1.1 and 0.1.10. – Randy Lai – 2014-08-30T10:53:28.023

@RandyLai Have you figured out how to get the correct order that can handle 0.0.19 and 0.0.2? – Ivan Wang – 2017-03-07T12:41:09.377

1this is what I used: git ls-remote --tags | sed 's|.*/\(.*\)$|\1|' | grep -v '\^' | sort -t. -k1,1nr -k2,2nr -k3,3nr – Randy Lai – 2017-03-15T01:07:36.730

5

git ls-remote --tags | awk -F'/' '/[0-9].[0-9].[0-9].*/ { print $3}' | sort -nr | head -n1

Using github api:

curl https://api.github.com/repos/user/repo/tags | jq '.[] .name' | sort -nr | head -n1

These two will get you latest tag, you can increase the list by changing the value at n flag for head pipe. lets say, to get top 10 latest lists head -n10

f-society

Posted 2013-12-22T21:21:36.190

Reputation: 1 384

0

With Git 2.18 (Q2 2018), "git ls-remote" learned an option to "git ls-remote" learned an option to allow sorting its output based on the refnames being shown.

See commit 1fb20df (09 Apr 2018) by Harald Nordgren (HaraldNordgren).
(Merged by Junio C Hamano -- gitster -- in commit 6c0110f, 08 May 2018)

ls-remote: create '--sort' option

Create a '--sort' option for ls-remote, based on the one from for-each-ref.
This e.g. allows ref names to be sorted by version semantics, so that v1.2 is sorted before v1.10.

So check out those for-each-ref --sort options introduced in Git 2.0 and 2.8, because they apply now to git ls-remote --sort.

VonC

Posted 2013-12-22T21:21:36.190

Reputation: 823 003