By default Git displays tags in alphabetical order, but we can configure Git to sort tags by their version numbers.
For the short version, you can jump to Configure Git to Sort By Version in Reverse Order by Default
Alphabetical Order
By default the list of tags displayed when you run
git tag
will be in alphabetical order. This becomes problematic when you have a mix of one digit and two digit numbers.
Here you can see 8.10.0
appears between 8.1.1
and 8.2.0
when sorted alphabetically.
...
8.1.1
8.10.0
8.2.0
...
Version Order
We can improve this by telling Git to treat each tag refname as a version and sort based on those comparisons
git tag --sort='version:refname'
gives a response that displays 8.10.0
in the correct position when comparing version numbers
...
8.1.1
8.2.0
...
8.9.0
8.10.0
8.11.0
...
Reverse Version Order
We can display the largest version tags first by prefixing version
with a minus sign (-
)
git tag --sort='-version:refname'
...
8.11.0
8.10.0
8.9.0
...
8.2.0
8.1.1
...
Configure Git to Sort By Version in Reverse Order by Default
We can configure Git to sort by version in reverse order by default, by running the following line from the command line
git config --global tag.sort '-version:refname'
How This Works
The above line adds the following entry to our Global Git Configuration file.
[tag]
sort = -version:refname
Which will configure Git so that when we run
git tag
Git automatically behaves as if we had run
git tag --sort='-version:refname'
Leave a Reply