1

https://stackoverflow.com/a/3338145/391104

I have tried all the following methods to query the difference between the last two commits that changed a given file (not just the most recent two commits, but two specific commits that had a specific effect). Unfortunately, none works for me.

git version 1.8.3.1

$ git diff HEAD^^ HEAD main.c
$ git diff HEAD^^..HEAD -- main.c
$ git diff HEAD~2 HEAD -- main.c

The only method that worked is to provide two commit hash versions.

Question> Do you know why I cannot use HEAD above to compare?

Here is what I have tried:

$ git lg1 abc.xml |head -n 3
* 3f13aa1 - (3 hours ago) test 2 - guest
* d59d3bc - (8 months ago) test 1 - guest
* 3a19f36 - (1 year, 2 months ago) test 0 - guest

$ git diff d59d3bc..3f13aa1 abc.xml
diff --git a/abc.xml b/abc.xml
index 5db0595..2790562 100644
--- a/abc.xml
+++ b/abc.xml
@@ -40,6 +40,12 @@
     Hello
+    World

$ git diff HEAD~2 HEAD~1 abc.xml
$ git diff HEAD^^ HEAD abc.xml
$ git diff HEAD^^..HEAD abc.xml
$ git diff HEAD~..HEAD abc.xml
torek
  • 448,244
  • 59
  • 642
  • 775
q0987
  • 34,938
  • 69
  • 242
  • 387
  • When you say "none works" why not? Do you get an error, if so can you please paste the output? – Cory Kramer Jul 08 '22 at 18:54
  • @CoryKramer, I just updated my OP. – q0987 Jul 08 '22 at 19:05
  • Note that `d59d3bc..3f13aa1` is `HEAD~..HEAD`. You're trying different revisions. Use `git rev-parse ` to check which commits you're comparing. Use `git log -p` to be sure there's differences in the commits you're comparing. – Schwern Jul 08 '22 at 19:15
  • @Schwern, I tried "HEAD~..HEAD" and it shows no difference. – q0987 Jul 08 '22 at 19:18
  • 1
    @q0987 And what do `git rev-parse HEAD` and `git rev-parse HEAD~` say? Are they 3f13aa1 and d59d3bc? – Schwern Jul 08 '22 at 19:27
  • @Schwern, no. I think Mureinik has answered the question for me. Thank you! – q0987 Jul 08 '22 at 19:40

1 Answers1

5

HEAD is not the last commit on a file - it's the last commit in the branch that's currently checked out. If the last few commits didn't affect the file in question, using HEAD will indeed return an empty output.

If you want to see what the latest commit did to a file, using the -p flag of git log may be easier:

git log -n1 -p abc.xml
Mureinik
  • 297,002
  • 52
  • 306
  • 350