How to get last Git tag matching regex criteria

Viewed 50469

I need Git command to get/find last tag starting with 'v' to get last versioning commit (I am using tags with v letter at the beginning to tag next application version (example: v0.9.1beta).

Is there any way to do it?

9 Answers

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*

Also with git describe you could get the latest tag not just reachable from HEAD with :

git describe --match "v*" --abbrev=0 --tags $(git rev-list --tags --max-count=1)

Something more complex would be along the lines of:

/v[0-9]+(\.[0-9]+).*/
Related