2

When using git diff to look at what has changed between two branches I often see unexpected results. I would like to understand why git sometimes considers a line changed when the contents of the line are identical in both files. Is there something hidden that is changing? Or is git doing something when it compares the files? This isn't always the case, but I find it happening on occasion and would love to understand why?

On master...

function a() {
  do_something();
}

On develop...

function a() {
  do_something();
}

function b() {
  do_something_else();
}

git diff example 1

-  }
+  }
+  
+  function b() {
+    do_something();
+  }

git diff example 2

Occasionally I will also see this type of output. (Note the last line is not an addition.)

+  }
+  
+  function b() {
+    do_something();
+  }
   }

I would expect the output in both of these examples to instead be the following:

+  
+  function b() {
+    do_something();
+  }

Can anyone explain what is going on here and as a bonus how to prevent it?

aprice42
  • 23
  • 4
  • 1
    Check for white-space differences: e.g., perhaps the original line has a tab instead of N spaces, or the replacement line has N spaces instead of a tab. You can also see this as a result of trailing white-space changes, but `git diff` normally highlights those with color to make them visible. – torek May 02 '19 at 02:08
  • Your diff-examples are botched. Since you remove `$do_something_else;` from `a()` AND also remove the `()` from the calls at least two removal and one addition lines are missing. – A.H. May 02 '19 at 12:14
  • @A.H. thanks for pointing that out! I had written the code example one way and changed course after I began writing the diffs. I updated the code example. – aprice42 May 02 '19 at 16:53

1 Answers1

1

To see differences without considering whitespaces, you can use git diff -w: that will focus on the more meaningful difference in your case.

Make sure to use the latest Git, as the diff heuristic has changed in the past.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250