Learn back-end development by writing real code

Boot.dev Blog ยป Open-Source ยป View Git Tags With Semver Ordering

View Git Tags with Semver Ordering

By Lane Wagner on Feb 9, 2021

Curated backend podcasts, videos and articles. All free.

If you're looking to become a backend developer, or just stay up-to-date with the latest backend technologies and trends, you found the right place. Subscribe below to get a copy of our newsletter, The Boot.dev Beat, each month in your inbox. No spam, no sponsors, totally free.

If you’re like me, you wish all Git tags adhered to the Semantic Versioning standard. Unfortunately, Semver is just a convention, so Git tags can basically be any string of text. By default when you use the git tag command, your output will be in alphabetical order. Being a gopher, almost all the projects I work on are tagged according to Semver standards, which means the default output is fairly useless.

To print all the Git tags in a project in Semver order, simply run git tag -l | sort -V.

Alternatively, if you’re on at least version 2 of Git, you won’t even need to use the sort command, just run:

git tag -l --sort=version:refname

If you want the latest tags at the top of the output, use -version to inverse the sort:

git tag -l --sort=-version:refname

If you want your global installation of Git to default to Semver sorting, you can use the following command as of Git v2.1+:

git config --global tag.sort version:refname

๐Ÿ”— Examples of Git standard output

๐Ÿ”— Default alphabetical sorting

v0.0.0
v0.0.1
v0.0.12
v0.0.2
v0.1.0
v0.10.0
v1.0.0
v1.1.1
v1.11.0
v1.12.0
v10.0.0
v2.0.0

๐Ÿ”— Semver sorting

v0.0.0
v0.0.1
v0.0.2
v0.0.12
v0.1.0
v0.10.0
v1.0.0
v1.1.1
v1.11.0
v1.12.0
v2.0.0
v10.0.0

Find a problem with this article?

Report an issue on GitHub