DEV Community

Jean-Yves Gastaud
Jean-Yves Gastaud

Posted on • Originally published at gastaud.io on

Sorting tags from a Git repo using semantic versioning

A quick post today to tell you about a recent discovery in Git's sorting functions and tags based on semantic versioning.

By default, if you run the git tag -l command, Git will do an alphabetical sort.

However, this sorting gives confusing results when the tags use semantic versioning notation.

git tag -l
v2.6.1
v2.6.10
v2.6.11
v2.6.12
v2.6.2
v2.6.3
v2.6.4
v2.6.5
v2.6.6
v2.6.7
v2.6.8
v2.6.9
v2.7.0

Enter fullscreen mode Exit fullscreen mode

Notice here the sequence v2.6.10, v2.6.11 and v2.6.12 which is interposed between version v2.6.1 and v2.6.2.

To solve this problem, it is possible to use the --sort function with the version attribute in Git.

git tag --sort=version:refname

Enter fullscreen mode Exit fullscreen mode

This will display the sorted results consistently:

git tag --sort=version:refname
v2.6.1
v2.6.2
v2.6.3
v2.6.4
v2.6.5
v2.6.6
v2.6.7
v2.6.8
v2.6.9
v2.6.10
v2.6.11
v2.6.12
v2.7.0

Enter fullscreen mode Exit fullscreen mode

Top comments (0)