Reputation: 231
There are some tags:
first
second
third
fourth
I need to get the tag before "second" (third).
Upvotes: 20
Views: 19548
Reputation: 505
Following command also can be used:
git tag --sort=-creatordate | head -n 3 | tail -n 2
Upvotes: 1
Reputation: 61
I found out the hard way that the accepted answer to this question only finds tags reachable from the commit history for the branch you are on (which is fine for many use-cases).
I needed to get prior commit regardless of current branch history, so this solution worked more wholistically.
git for-each-ref --sort=-creatordate --format '%(refname:short)' refs/tags | sed -n '/second/{n;p;}'
Upvotes: 3
Reputation: 1620
Alternative solution with any sorting you like to get the next tag after the current one:
git tag --sort=-creatordate | grep -A 1 second | tail -n 1
git tag --sort=-creatordate
prints all tags from newest to oldest by creator date: "first second third fourth". Choose sorting option wisely.grep -A 1 second
filter only "second" and one following line after it, i.e "third".tail -n 1
prints one last line from "second third" and we have finally get the "third".Upvotes: 2
Reputation: 970
Or between the last two tags
git log $(git describe --abbrev=0 --tags $(git describe --abbrev=0)^)...$(git describe --abbrev=0)
Upvotes: 9