Search code examples
gitgit-diffgit-tag

Git diff between latest and previous tag


I want to do a diff just like this one.

The only difference is that i don't want to inform the tag names 'manually' i want them to be retrieved by git commands.

I know that git describe --tags returns my latest tag. But what about the previous one? How to get it?

Basically what i want is:

$ git diff $(git_command_to_get_previous_tag) $(git describe --tags)

And what i don't want:

$ git diff 1.0 2.0

Solution

  • You can get the latest tag using:

    git tag --sort version:refname | tail -n 1
    

    And the previous tag using:

    git tag --sort version:refname | tail -n 2 | head -n 1
    

    Putting it together, you can get a diff using this:

    git diff $(git tag --sort version:refname | tail -n 2 | head -n 1) $(git tag --sort version:refname | tail -n 1)