Home > Blockchain >  How to compare two commits without providing exact commit hash versions?
How to compare two commits without providing exact commit hash versions?

Time:07-09

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

CodePudding user response:

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
  • Related