Skip to content

Instantly share code, notes, and snippets.

@loisaidasam
Last active July 8, 2026 21:11
Show Gist options
  • Select an option

  • Save loisaidasam/b1e6879f3deb495c22cc to your computer and use it in GitHub Desktop.

Select an option

Save loisaidasam/b1e6879f3deb495c22cc to your computer and use it in GitHub Desktop.
Sort git tags by semver

If you're like me and you use semver for versioning your tags, you probably hate when you do this:

$ git tag -l
0.1.0
0.10.0
0.2.0
0.3.0
0.3.1
0.4.0
0.5.0
0.6.0
0.7.0
0.7.1
0.7.2
0.8.0
0.8.1
0.8.2
0.8.3
0.8.4
0.8.5
0.8.6
0.9.0

because the 0.10.0 tag is hiding way up near the top and you might not even see it.

Here's the solution, create a file on your $PATH (maybe in your ~/bin dir) called git-tag-sort with the contents of the file below, and you should be able to do this:

$ git tag-sort 
0.1.0
0.2.0
0.3.0
0.3.1
0.4.0
0.5.0
0.6.0
0.7.0
0.7.1
0.7.2
0.8.0
0.8.1
0.8.2
0.8.3
0.8.4
0.8.5
0.8.6
0.9.0
0.10.0

Have fun!

#!/bin/bash
git tag --sort=v:refname
@esunilkumare

Copy link
Copy Markdown

git tag | sort -t "." -k1,1n -k2,2n -k3,3n

Thanks this worked.

@designermonkey

Copy link
Copy Markdown
git tag | tr - \~ | sort -V | tr \~ -

I cannot believe after trying to figure this out I finally saw that line and it blew my mind.

@paulcervov

Copy link
Copy Markdown

git tag | tr - \~ | sort -V | tr \~ -

Only this πŸ‘† works correct.

0.0.1
0.0.2-develop
0.0.2

πŸ‘ Thanks!

@ArwynFr

ArwynFr commented Aug 3, 2025

Copy link
Copy Markdown

Just my two cents for lazy readers:

  • If you're using powershell or cmd, you need to invoke bash: bash -c "git tag | tr - \~ | sort -V | tr \~ -"
  • You can have the newest version first by reversing sort: git tag | tr - \~ | sort -rV | tr \~ -

@juaniyyoo

Copy link
Copy Markdown

Or just configure git to do this anyway.

git config --global tag.sort version:refname

Then git tag sorts this way by default.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment