A lot of people use semantic versioning with their git tags. Like this:

$ git tags
v0.1.3
v0.3.12
v1.5.0
v0.10.7

Getting the most recent version (or all of the versions in order) is a pain because sorting them doesn’t give the correct semantic order:

$ git tags | sort
v0.1.3
v0.10.7
v0.3.12
v1.5.0

The v0.10.7 mistakenly appears before the v0.3.12 because '1' < '3'.



If your versions do not have the v prefix, you can simply use:

$ git tags | tr '.' ' ' | sort -k1,1 -k2,2 -k3,3 -n | tr ' ' '.'
0.1.3
0.3.12
0.10.7
1.5.0

However, if your versions do contain the v prefix (or any other prefix for that matter) you will have to strip it out before the sort, like:

$ git tags | tr '.' ' ' | tr -d 'v' | sort -k1,1 -k2,2 -k3,3 -n | tr ' ' '.' | xargs -I{} -n 1 echo "v{}"
v0.1.3
v0.3.12
v0.10.7
v1.5.0

Notice that the prefix is stripped off with tr, but then appended after the sort with xargs.



Other Tips

  1. You can use head or tail to find the most recent or oldest versions respectively.
  2. Add -r to the sort command to get the versions in reverse.
  3. Use grep to filter the tags before performing the sort. This is helpful if you have other tags that are not versions or should not be included in the output.