Purple Hexagon
Purple Hexagon

Reputation: 3578

Git displaying a sorted list of lightweight tags using a sort

I have a load of lightweight tags that have been created in a repo which I need to sort and return the top 5 tags. It is my understanding that lightweight tags can't be sorted by date created as they are just a pointer to a commit.

I want to be able to (for use in a bash script) get the last 5 (This will be in alphanumeric order)

So for example if I ran these commands (Not that I would ever add tags like this, just to highlight what I want):

git tag 1.0.0
git tag 1.4.0
git tag 1.2.0
git tag 1.6.0
git tag 1.7.0
git tag 1.8.0

I would want a list like:

I know I can do

git tag | head -n5

but these are not sorted correctly, this is the first 5 tags.

I know if these were annotated tags I could use git describe or do something like

git for-each-ref --sort=-taggerdate \
--format '%(refname:short) %(taggerdate:raw)' refs/tags

but this doesn't work for the lightweight tags for the afore mentioned reason.

So is there a way to list the last 5 (doesn't matter if sorted numerically or by date as this will always be the same) lightweight tags?

I know I could probably do this https://stackoverflow.com/a/21032471/1185180 to convert them, but I would rather not....

Upvotes: 0

Views: 564

Answers (1)

WKPlus
WKPlus

Reputation: 7255

> git tag
1.0.0
1.2.0
1.4.0
1.6.0
1.7.0
1.8.0
> git tag | sort -r | head -5
1.8.0
1.7.0
1.6.0
1.4.0
1.2.0

Upvotes: 1

Related Questions