5

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
Community
  • 1
  • 1
Mateus Cerqueira
  • 457
  • 4
  • 13

2 Answers2

9

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)
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • That worked! Thanks ! The command is a little longer than i expected but there's no problem there :) – Mateus Cerqueira Jan 14 '16 at 22:25
  • 1
    how could it be work on windows ? tail command is not recognized on windows. – Vedank Kulshrestha Mar 05 '18 at 10:08
  • @VedankKulshrestha you can do almost the same thing in PowerShell: `git diff tags/$(git tag -l --sort=-version:refname v* | select -first 1) tags/$(git tag -l --sort=-version:refname v* | select -first 2 | select -last 1) --name-only` – jgstew Feb 17 '22 at 22:17
0

I needed to do the same, but with PowerShell instead of Bash. The general method is the same though.

Fetch all tags in case there are new ones:

git fetch --all --tags

Get the latest tag using:

git tag -l --sort=-version:refname | select -first 1

Get the previous tag using:

git tag -l --sort=-version:refname | select -first 2 | select -last 1

Put it together:

git diff $(git tag -l --sort=-version:refname | select -first 1) $(git tag -l --sort=-version:refname | select -first 2 | select -last 1)

My actual example use case:

git fetch --all --tags > $null; git diff tags/$(git tag -l --sort=-version:refname v* | select -first 1) tags/$(git tag -l --sort=-version:refname v* | select -first 2 | select -last 1) --name-only | Select-String -Pattern ".*\.xml" | Write-Host

This will get all remote tags in case there are new ones, redirecting output to $null so it does not show up, get the list of files changed between the most recent tags that start with v, then filter the list of files to only return those that end in .xml

jgstew
  • 131
  • 2
  • 9