How to get last Git tag matching regex criteria

Solution 1:

I'm using the following command for this:

git describe --match "v[0-9]*" --abbrev=4 HEAD

It will also modify the version if you did something with the source tree since your last versioned tag.

Please note that this is not a regex but a glob but works for the provided example.

Solution 2:

git tag -l -n v*

Solution 3:

The problem with using git describe as the other answers do is that git describe will show you tags that are reachable from HEAD (or the commit you specify.)

Imagine you have 3 tags, v1, v2, and v3. If HEAD is at a point between v2 and v3, git describe would return v2 rather than v3.

If you actually want the latest tag, first of all you need annotated tags as lightweight tags have no date metadata.

Then this command will do it:

git for-each-ref --sort=-taggerdate --count=1 refs/tags/v*