Why a diff sometimes highlights the wrong lines

A diff tool does not know what you typed. It only sees the old and new text and works out one set of edits that turns one into the other. When several sets are valid, the one it picks may not match what you did.

The same change, two results

Most of the time every algorithm agrees. They disagree when a file has many identical lines, such as lone braces, blank lines or repeated return statements. In this example a developer moved a helper to the top of the file, renamed it from Square to Cube, and deleted one line from PrintValue. The old file:

using System;

// Prints a value with a label
static void PrintValue(string label, int value)
{
    for (int i = 0; i < 3; i++)
    {
        Console.WriteLine("Result:");
        Console.WriteLine(label + " = " + value);
    }
}

static int Square(int n)
{
    if (n > 0)
    {
        return n * n;
    }
    return 0;
}

static void Main()
{
    PrintValue("area", Square(12));
}

This is git diff --diff-algorithm=myers, the git default:

@@ -1,25 +1,24 @@ using System; -// Prints a value with a label-static void PrintValue(string label, int value)+static int Cube(int n) {-    for (int i = 0; i < 3; i++)+    if (n > 0)     {-        Console.WriteLine("Result:");-        Console.WriteLine(label + " = " + value);+        return n * n * n;     }+    return 0; } -static int Square(int n)+// Prints a value with a label+static void PrintValue(string label, int value) {-    if (n > 0)+    for (int i = 0; i < 3; i++)     {-        return n * n;+        Console.WriteLine(label + " = " + value);     }-    return 0; }  static void Main() {-    PrintValue("area", Square(12));+    PrintValue("volume", Cube(12)); }

It is correct, but it reads as if PrintValue became Cube and Square became PrintValue. Braces were matched across different functions because they are identical text. The same change with --diff-algorithm=patience:

@@ -1,25 +1,24 @@ using System; +static int Cube(int n)+{+    if (n > 0)+    {+        return n * n * n;+    }+    return 0;+}+ // Prints a value with a label static void PrintValue(string label, int value) {     for (int i = 0; i < 3; i++)     {-        Console.WriteLine("Result:");         Console.WriteLine(label + " = " + value);     } } -static int Square(int n)-{-    if (n > 0)-    {-        return n * n;-    }-    return 0;-}- static void Main() {-    PrintValue("area", Square(12));+    PrintValue("volume", Cube(12)); }

Now the diff says what happened: a new function at the top, one deleted line, the old function removed and one call updated. git diff --stat reports 9 insertions and 10 deletions for Myers, and 10 and 11 for patience. The shortest diff is not always the clearest one.

How the algorithms differ

Getting a clearer diff in git

None of these options change your files or commits, only how the difference is displayed, so it is safe to try each one.

How Compare Codes lines up rows

Compare Codes uses the patience approach:

  1. Identical lines at the start and end of both inputs are matched first.
  2. Non blank lines that appear exactly once on each side become anchors, keeping the longest set that stays in order.
  3. Steps 1 and 2 repeat inside each gap between anchors.
  4. A gap with no unique lines falls back to a full longest common subsequence comparison. Above about seven million line pairs it switches to a faster pass that looks up to 120 lines ahead, so the page stays responsive.

On the example above it gives the same result as git's patience diff. Inside a modified row it splits both lines into words, spaces and symbols, so in the Main call only area, volume, Square and Cube are highlighted.

Habits that keep diffs readable