lime
lime

Reputation: 7111

Get the two most recent git tags (and the log between them)

I am looking for a git command that would show me the git log between the two most recent tags in the current branch.

I.e. if the two most recent tags are build_341 and build_342, then I would want to end up with the output of git log build_341..build_342

I know that I can get the most recent tag using git describe --abbrev=0, but I don't know how to show the second most recent tag.

Upvotes: 0

Views: 145

Answers (2)

Sam H
Sam H

Reputation: 889

If you have the situation where the HEAD is a merge commit, I found this solution to return the most recent tag among the parent commits, using PowerShell.

# Find the latest tag for each of the current commit's parents.
$parentsTags = git rev-parse HEAD^@ | ForEach-Object { 
    git describe --tags --abbrev=0 $_
}

# Out of all the tags, sorted oldest-to-newest, find the last one that's also one of the parents' tags.
$nextLatestTag = git for-each-ref --sort=creatordate --format '%(refname)' refs/tags `
    | Split-Path -Leaf `
    | Where-Object { $parentsTags.Contains($_) } `
    | Select-Object -Last 1

Upvotes: 0

lime
lime

Reputation: 7111

Well, it's possible to get the second most recent tag using:

git describe --abbrev=0 $(git describe --abbrev=0)^

So I can get a log between the two most recent tags using:

git log $(git describe --abbrev=0 $(git describe --abbrev=0)^)..$(git describe --abbrev=0)

Not pretty, but it seems to work (as long as your shell supports $() command substiution). Other answers are welcome.

Upvotes: 1

Related Questions