ThomasReggi
ThomasReggi

Reputation: 59345

Get last git tag from a given tag

Given a git tag (ex. v.0.1.0) I would like a bash command that will give me the previous chronological tag (ex. v.0.0.5). Here I'm using semver for version control and you can see that I can't just decrement the numbers. I need the previous last tag that was given to the repo. Thoughts?

I've tried git describe --tags and its giving me the last tag kindof. But nothing this specific.

Upvotes: 0

Views: 1190

Answers (1)

larsks
larsks

Reputation: 311397

There are several solutions out there for listing your tags in chronological order. The one that I often use is:

git for-each-ref --sort=authordate --format '%(refname) %(authordate)' refs/tags

This works fine if all your tags are "lightweight" tags. If all your tags are annotated tags, use taggerdate instead of authordate. With your tags listed in chronological order, it should be easy to find the tag chronologically prior to any other given tag.

See also:

Upvotes: 2

Related Questions