Last Updated: February 25, 2016
·
3.36K
· loisaidasam

Sort your git tags by version! Version Sort to the rescue!

Say you use Semantic Versioning (http://semver.org/) for your git project's releases, and your list of tags is growing longer and longer.

Now it's time for you to make a new tag, but you don't remember what the last tag was.

You try:

$ tag -l | sort

but then you see:

0.0.1
0.0.10
0.0.2
0.0.3
0.0.4
0.0.5
0.0.6
0.0.7
0.0.8
0.0.9
0.1.0

Note: that 0.0.10 comes after 0.0.1 - that ain't right!

You check the man page, and neither dictionary order, general numeric sort, nor numeric sort seem to work. GAH!!! The frustration!

You think to yourself, should I write a simple script that parses out the dots and then does comparison? No, someone has to have done this before... a little more Googling... Still nothing.

version sort to the rescue!

$ tag -l | sort -V

Now OS X's sort doesn't come with the -V flag, so if you want to use it there you'll have to use macports or brew or something to install coreutils, then you should be able to do:

$ tag -l | gsort -V

Have fun kiddos!

2 Responses
Add your response

This doesn't work 100% correctly for Semantic Versioning and pre-releases. The prerelease will be come after the release.

Above will result in:
v2.0.0
v2.0.0-rc0
v2.0.0-rc1
v2.0.0-rc2
v2.0.0-rc3
v2.0.0-rc4
v2.0.1

over 1 year ago ·

There's also:

git tag --sort=v:refname

but it suffers from the same issue @petervanderdoes mentions.

over 1 year ago ·