• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (deprecated)
You are here: Home / Dev Tips / Sort Git Tags by Version

Sort Git Tags by Version

Last updated on October 21, 2023 by Sal Ferrarello

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'
Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP and JavaScript developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a principal engineer.

Filed Under: Dev Tips, Solution Tagged With: Git, gitconfig

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2026 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in