Get last git tag matching string

1.7k views Asked by At

In git, let's say I have multiple tags with revision numbers, something like:

v1.1.0
v1.2.0
v1.2.1
v1.2.3

I'm interested in getting the last version number from the tag. If I do

git describe --tags --match "v1.2.*"

I will get v1.2.0 as the result, but I really want v1.2.3

2

There are 2 answers

2
VonC On

If you want to list all tags, using a specifc order, you can (with git 2.0+) use a sort option.
See "How to sort git tags?"

git tag -l --sort=refname "v1.2.*"
# or (note the '-' sign in '-version' to reverse the order)
git tag -l --sort=-version:refname "v1.2.*"  

In each case, the first result should be v1.2.3

0
barbariania On

Also possible with git describe:

git describe --tags --match "v1.2.*" $(git rev-list --tags --max-count=1)

Helped this: https://stackoverflow.com/a/30811684/3598880