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

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay