aprice42
aprice42

Reputation: 23

Why does git diff consider something changed when is the same on both branches?

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?

Upvotes: 2

Views: 640

Answers (1)

VonC
VonC

Reputation: 1329692

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.

Upvotes: 1

Related Questions