George Edwards
George Edwards

Reputation: 9229

Can you get the actual differences between two git commits?

In the scenario where the first commit, is adding file X, second commit, you remove X and the third commit you re-add it.

If you run git diff, then you get X deleted, X added.

Is there a way to analyse this set of changes and get the result, "no change" ?

I am actually interested in the line changes, in my real example, I get this response:

index 5988d3c..eaf3238 100644
--- a/.babelrc
+++ b/.babelrc
@@ -1,3 +1,3 @@
-{
-  "presets": ["es2015", "stage-0"]
-}
+{
+  "presets": ["es2015", "stage-0"]
+}

which seems to show the exact same lines added and subtracted.

Edit:

Here is the output of diff -R

--- b/.babelrc
+++ a/.babelrc
@@ -1,3 +1,3 @@
-{
-  "presets": ["es2015", "stage-0"]
-}
+{^M
+  "presets": ["es2015", "stage-0"]^M
+}^M

Upvotes: 3

Views: 75

Answers (3)

Adam Nierzad
Adam Nierzad

Reputation: 942

You can simply do git diff [first commit] [third commit] and this will return no difference.

For example here is the git log of my test repository.

commit 368c6c0bc4169b8f482761b036da2284f937579d
Author: Adam
Date:   Fri May 6 10:17:45 2016 +0100

    Edited file back.

commit 54d9885f860b56c34718387206397c703eb37b36
Author: Adam
Date:   Fri May 6 10:16:51 2016 +0100

    Edited file.

commit 681f117fd095c7a99d631a1819db855d7fd9e6e8
Author: Adam
Date:   Fri May 6 10:16:09 2016 +0100

    Added file.

When I compare the first and third commits using git diff 681f117fd095c7a99d631a1819db855d7fd9e6e8 368c6c0bc4169b8f482761b036da2284f937579d the result is empty.

However if I compare the first and second commit using git diff 681f117fd095c7a99d631a1819db855d7fd9e6e8 54d9885f860b56c34718387206397c703eb37b36 the result is:

diff --git a/file.txt b/file.txt
index c9a0e4f..76af740 100644
--- a/file.txt
+++ b/file.txt
@@ -1,6 +1,6 @@
 I won't edit this line.

-I will edit this line.
+I have edited this line.

 I won't edit this line.

If you aren't getting an empty change set but the lines read the same then there must be some difference in characters within the line, for example a tab being used instead of a space or a different line ending.

You can use git diff -w to ignore white space when performing the diff. Documentation: https://git-scm.com/docs/git-diff

I hope this helps.

Upvotes: 2

Amol Udage
Amol Udage

Reputation: 3075

You can simply try using following command It will show you difference between two commits..

git diff first_commit_id(hash) second_commit_id(hash)

you can find commit_id(hash) by typing git log command..

Example:

git diff effd192760b957b9a65b011249c122f8434ba7cf effd192760b957b9a67b011249c122f8434ba7cf

Upvotes: 1

dimid
dimid

Reputation: 7659

As I've said in the comments, it's a whitespace issue. Use dos2unix to get rid of ^M. If you want to ignore whitespace use git diff -w, although I wouldn't recommend it.

Upvotes: 0

Related Questions