How to get previous tag in git


I want to be able to roll back to a previous git tag without having to know what the current tag is or what the previous tag is.

I know how to get the latest tag:

git describe --abbrev=0 --tags

but how would I get the tag right before it?


I couldn’t find any resources online for doing this, so here is what I found to work:

 git describe --abbrev=0 --tags `git rev-list --tags --skip=1  --max-count=1`

The subcommand gets the hash of one recent tag. The --skip=1 means the “latest” tag will be skipped and the “previous” tag to the latest will be returned.

If current tag is 1.1.5, the previous tag number returned would be 1.1.4

Answered By – Kevin Jantzer

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published