Skip to content

Instantly share code, notes, and snippets.

@loisaidasam
Last active October 19, 2023 16:07
Show Gist options
  • Save loisaidasam/b1e6879f3deb495c22cc to your computer and use it in GitHub Desktop.
Save loisaidasam/b1e6879f3deb495c22cc to your computer and use it in GitHub Desktop.
Sort git tags by semver

If you're like me and you use semver for versioning your tags, you probably hate when you do this:

$ git tag -l
0.1.0
0.10.0
0.2.0
0.3.0
0.3.1
0.4.0
0.5.0
0.6.0
0.7.0
0.7.1
0.7.2
0.8.0
0.8.1
0.8.2
0.8.3
0.8.4
0.8.5
0.8.6
0.9.0

because the 0.10.0 tag is hiding way up near the top and you might not even see it.

Here's the solution, create a file on your $PATH (maybe in your ~/bin dir) called git-tag-sort with the contents of the file below, and you should be able to do this:

$ git tag-sort 
0.1.0
0.2.0
0.3.0
0.3.1
0.4.0
0.5.0
0.6.0
0.7.0
0.7.1
0.7.2
0.8.0
0.8.1
0.8.2
0.8.3
0.8.4
0.8.5
0.8.6
0.9.0
0.10.0

Have fun!

#!/bin/bash
git tag --sort=v:refname
@thdoan
Copy link

thdoan commented Nov 5, 2015

This won't work in Git for Windows. Do this instead: git tag | sort -t "." -k1,1n -k2,2n -k3,3n. This will set the dot as a column separator, then do a numeric sort on the first column, then the second column, then the third column.

@gammamatrix
Copy link

Instead of creating a new script, you can just create a git alias:

git config --global alias.versions 'tag --sort=v:refname'

Now, you only need to run:

git versions

You will then see your all your sorted tags:

1.0.0
1.0.1
1.0.2
1.1

@tremby
Copy link

tremby commented Aug 28, 2016

Or just configure git to do this anyway.

git config --global tag.sort version:refname

Then git tag sorts this way by default.

@thiagofigueiro
Copy link

This --sort v:refname work with prerelease versions; e.g.:

$ git tag --sort=v:refname
2.1.0
2.1.0-rc1
2.1.0-rc3
2.1.0-rc4
2.1.0-rc5
2.1.0-rc6
2.1.0-rc7
2.1.1
2.1.1-rc1

2.1.0 and 2.1.1 should come after their release candidates but they don't.

@timhughes
Copy link

$ git tag |sort --version-sort
1.0.0
1.1.0
1.2.0
1.2.1
1.3.0
1.4.0
1.5.0
1.6.0
1.7.0
1.8
1.8.0
1.9.0
1.10.0
1.11.0
1.12.0
1.13.0
1.14.0
1.15.0
1.16.0
1.17.0
1.18.0
1.19.0
1.20.0
1.21.0
1.22.0
1.23.0
1.23.1
1.23.2
1.23.3
2.0.0beta1
2.0.0beta2
2.0.0beta3

@meeDamian
Copy link

meeDamian commented Aug 26, 2019

This won't work in Git for Windows. Do this instead: git tag | sort -t "." -k1,1n -k2,2n -k3,3n. This will set the dot as a column separator, then do a numeric sort on the first column, then the second column, then the third column.

This is the only valid answer. Everything else breaks down when anything over major+minor+patch is used.

I take that back. Doing that is completely broken in hard to explain ways in some cases… Example:

git tag | sort -t "." -k1,1n -k2,2n -k3,3n
…
v0.12.0-rc2
v2.12.0
v2.12.0-rc0
v2.12.0-rc1
v2.12.0-rc2
v2.12.0-rc3
v2.12.0-rc4
v0.12.1
v2.12.1
v0.12.2
v0.12.3
…

🤷🏻‍♂️

The thing that finally worked for me was using Debian-inspired ~ instead of - for the time of sorting:

git tag | tr - \~ | sort -V | tr \~ -

@esunilkumare
Copy link

git tag | sort -t "." -k1,1n -k2,2n -k3,3n

Thanks this worked.

@designermonkey
Copy link

git tag | tr - \~ | sort -V | tr \~ -

I cannot believe after trying to figure this out I finally saw that line and it blew my mind.

@paulcervov
Copy link

git tag | tr - \~ | sort -V | tr \~ -

Only this 👆 works correct.

0.0.1
0.0.2-develop
0.0.2

👏 Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment