Skip to main content
  1. Posts/

Git Diff Setup

·360 words·2 mins·
Git Diff
Table of Contents

When we run git-diff, git uses its internal diff algorithm to generate diff for changed files.

The git internal diff algorithms
#

The default diff algorithm used by git-diff is myers diff. However, myers diff algorithm may not generate meaningful diffs for code changes. So the patience diff and histogram diff algorithm is also added later.

This post shows an example where myers diff does not make much sense and patience or histogram diff produce better diffs. It seems that histogram diff generally produces better diff.

To use patience or histogram diff in git diff, use the following command:

git diff --patience
git diff --histogram

However, this does not mean that histogram/patience diff always generate better diffs. An example is given here1.

f1:

aaaaaa
aaaaaa
bbbbbb
bbbbbb
cccccc
cccccc
abc

f2:

abc
aaaaaa
aaaaaa
bbbbbb
bbbbbb
cccccc
cccccc

The output of git diff --patience f1 f2 is:

diff --git a/f1 b/f2
index 3299d68..accc3bd 100644
--- a/f1
+++ b/f2
@@ -1,7 +1,7 @@
-aaaaaa
-aaaaaa
-bbbbbb
-bbbbbb
-cccccc
-cccccc
 abc
+aaaaaa
+aaaaaa
+bbbbbb
+bbbbbb
+cccccc
+cccccc

The output of git diff f1 f2 (remember that myers diff is used by default) is:

diff --git a/f1 b/f2
index 3299d68..accc3bd 100644
--- a/f1
+++ b/f2
@@ -1,7 +1,7 @@
+abc
 aaaaaa
 aaaaaa
 bbbbbb
 bbbbbb
 cccccc
 cccccc
-abc

In this case, result of myers diff make much more sense than that of patience diff.

Git external diff tools
#

Git can also utilize external diff tool via git difftool command. Note that git difftool has nothing to do with git diff (its naming is a bit confusing since it may make the users believe that git difftool chooses which tool git-diff uses).

You can run command git difftool --tool-help to check a list of supported external diff tools. To select a certain tool, use git difftool --tool <my-diff-tool>. For example, to use vimdiff, run git difftool --tool vimdiff.

Refs
#


  1. The example compares patience against myers diff, but histogram diff generates the same result as patience diff in this case. ↩︎

Related

Show Git Diff When Doing Git Commit
··260 words·2 mins
Git Nvim Vim Diff
Git Learning Notes (3)
··481 words·3 mins
Git Git Diff
Git Learning Notes (1)
··701 words·4 mins
Git Git Diff