I am new to the world of version control and have just started using git. I have the following output when I run the command git log
commit 3b33318e20a64f4395bba416fe60f50f9e0002d1
Author: pm
Date: Thu Jan 24 08:42:24 2013 +1300
added fourth line to test1
commit 37f2ce409cdc971958add1fcf6585064f5c0d61d
Author: pm
Date: Thu Jan 24 08:41:24 2013 +1300
first commit
I understand that git log
show the latest commit followed by the previous commit. Now if I run the command git diff HEAD HEAD~
which I understand to be "Show me the difference between the latest commit and the previous commit", I get the following output:
diff --git a/test1 b/test1
index c6f02c2..e41a5e4 100644
--- a/test1
+++ b/test1
@@ -1,4 +1,3 @@
This is a test document
This is the second line in the document
And the third
-Added a fourth line
It shows the minus symbol where I added a new line when I modified the file test1 however if I run the command as git diff HEAD~ HEAD
which I understand to be "Show me the difference between the second last commit and the latest commit", it shows me the following output:
diff --git a/test1 b/test1
index e41a5e4..c6f02c2 100644
--- a/test1
+++ b/test1
@@ -1,3 +1,4 @@
This is a test document
This is the second line in the document
And the third
+Added a fourth line
It shows that I added the fourth line with the plus symbol.
Does it matter how files are compared? I would have thought the manner in which you compare files would be "compare latest with previous" i.e. git diff HEAD HEAD~
git diff A B
lists the changes needed to get from A to B. If you swap the arguments, you get the reversed changes, as in your example.
You could argue that Git could know that A happened before B, and then produce the same output for git diff A B
and git diff B A
. However, IMHO that is not a good idea because of two reasons:
Showing the changes as how to get from left to right is more consistent. It also comes handy if you build scripts on top of it.
Sometimes it is not clear which commit is before another, for example:
C
/ \
A B
git diff A B
: Is A before B or B before A?
The order does indeed matter when passing the references to git diff. To see the diff in a past to future way you need:
git diff HEAD~ HEAD
Git doesn't do any guesswork for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With