How to read a git diff

The unified diff format is what git diff, patch files and most review tools produce. Once you know its few symbols, you can read a change in a terminal as quickly as in a visual tool.

A real example

This is git diff output after editing a small C# license validator. It is the same code the Sample button loads in Compare Codes, so you can open both views side by side.

diff --git a/LicenseValidator.cs b/LicenseValidator.csindex 687adae..1ac97e9 100644--- a/LicenseValidator.cs+++ b/LicenseValidator.cs@@ -4,17 +4,16 @@ public class LicenseValidator     {         if (string.IsNullOrWhiteSpace(code))         {-            return false;+            throw new ArgumentException("License code is required.");         } -        var normalized = code.Trim();-        var parts = normalized.Split('-');-        if (parts.Length != 4)+        var normalized = code.Trim().ToUpperInvariant();+        if (!normalized.Contains("-"))         {             return false;         } -        return normalized.Length == 19;+        return normalized.Length == 19 && normalized.Contains("-");     }      public string GetMessage()

The first four lines describe the file. Everything from the @@ line on is a hunk: one block of changes with a few unchanged lines around it. Changes far apart in a file produce several hunks.

The file header

LineMeaning
diff --git a/... b/...The file on both sides. a/ is the old version and b/ the new one. These are labels, not folders. A renamed file shows two different paths.
index 687adae..1ac97e9 100644Hashes of the old and new contents. git show 687adae prints that version. 100644 is a normal file and 100755 an executable one.
--- a/... and +++ b/...Lines starting with - belong to the old file and lines starting with + to the new one. A new file has /dev/null as its old side, and a deleted file as its new side.

The hunk header

@@ -4,17 +4,16 @@ public class LicenseValidator is the most useful line once you can read it:

A count of 1 is left out, so @@ -1 +1 @@ means one line on each side. A count of 0 means that side is empty, as in a new file: @@ -0,0 +1 @@. If git picks the wrong function name, set a language pattern in .gitattributes, such as *.cs diff=csharp.

Context, removed and added lines

Every line in a hunk starts with a space (unchanged), - (old file only) or + (new file only). There is no symbol for an edited line. It appears as a - line followed by a + line, and when several lines change together git prints all removed lines first, then all added lines.

Git shows three lines of context by default. With -U1 the same change splits into two hunks:

@@ -6,8 +6,7 @@ public class LicenseValidator         {-            return false;+            throw new ArgumentException("License code is required.");         } -        var normalized = code.Trim();-        var parts = normalized.Split('-');-        if (parts.Length != 4)+        var normalized = code.Trim().ToUpperInvariant();+        if (!normalized.Contains("-"))         {@@ -16,3 +15,3 @@ public class LicenseValidator -        return normalized.Length == 19;+        return normalized.Length == 19 && normalized.Contains("-");     }

In the second hunk header the old file is at line 16 and the new file at line 15. The first hunk removed one more line than it added, so everything after it moved up by one. Reading offsets this way helps you find the same code in both versions.

New, deleted and renamed files

This commit deletes legacy.txt, renames helpers.js to utils.js with one edit, and adds version.js:

diff --git a/legacy.txt b/legacy.txtdeleted file mode 100644index 3367afd..0000000--- a/legacy.txt+++ /dev/null@@ -1 +0,0 @@-olddiff --git a/helpers.js b/utils.jssimilarity index 79%rename from helpers.jsrename to utils.jsindex 4083766..9928863 100644--- a/helpers.js+++ b/utils.js@@ -2,7 +2,7 @@ line1 line2 line3 line4-line5+line5 changed line6 line7 line8diff --git a/version.js b/version.jsnew file mode 100644index 0000000..2bac970--- /dev/null+++ b/version.js@@ -0,0 +1 @@+export const VERSION = "1.0";

similarity index 79% is how much of the renamed file stayed the same. Git detects renames by content, so a file renamed and heavily edited in one commit may appear as a delete plus a new file. Commit the rename first and the edits second if you want reviewers to see it clearly.

\ No newline at end of file means the line above has no line break after it. Binary files a/logo.png and b/logo.png differ means git will not show a text diff for that file.

Commands that produce diffs

CommandWhat it compares
git diffWorking files against the staging area: changes you have not staged yet.
git diff --stagedStaging area against the last commit: what your next commit will contain.
git diff HEADWorking files against the last commit, staged or not.
git diff main...featureChanges on feature since it branched from main, close to what a pull request shows.
git show <commit>The message and diff of one commit.
git log -p -- path/to/fileEvery commit that touched one file, each with its diff.
git diff --no-index a.txt b.txtAny two files on disk, even outside a repository.

Options worth knowing: --stat gives one line per file with a count of changed lines, -W shows the whole function around each change, -w ignores whitespace, and --word-diff marks changed words inside a line. Here is the first part of the example with --word-diff:

        if (string.IsNullOrWhiteSpace(code))
        {
            [-return false;-]{+throw new ArgumentException("License code is required.");+}
        }

        var normalized = [-code.Trim();-]
[-        var parts = normalized.Split('-');-]{+code.Trim().ToUpperInvariant();+}
        if [-(parts.Length != 4)-]{+(!normalized.Contains("-"))+}

Removed words are wrapped in [- -] and added words in {+ +}.

When a side by side view is easier

Unified diffs are compact, but they get hard to read when many lines change together, because all removed lines come before all added lines. Paste both versions into Compare Codes to see each old line next to its replacement. To judge whether this example change is actually correct, follow the block by block review in how to review a code change.