Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Objects, Refs and Internals

Unified diff format

Reference for the diff format Git prints: file headers, extended header lines, @@ hunk markers, context lines, rename and binary patches, and combined diffs.

The text printed by git diff, git show, git log -p and git format-patch is a unified diff with Git's extensions. It is meant for both people and programs: git apply and patch read it back, and code-review tools parse it to draw side-by-side views. Knowing the layout lets you read any diff quickly and write tools that process one.

A diff is a sequence of per-file sections. Each section has a header that names the files and describes what happened to them (changed, created, deleted, renamed, mode change), followed by zero or more hunks: the groups of changed lines with a few lines of context around them.

Terminal
git diff                  # working tree versus index
git diff --cached         # index versus HEAD
git show HEAD             # message plus the diff of a commit
git diff --stat           # summary instead of lines

Try it

Bash Read every part of a diff

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash A combined diff for a merge commit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Anatomy of a file section

TEXT
diff --git a/n.txt b/n.txt
index 1a2b3c4..5d6e7f8 100644
--- a/n.txt
+++ b/n.txt
@@ -1,6 +1,6 @@ optional function context
 context line
-removed line
+added line
 context line
Line Meaning
diff --git a/X b/Y Start of a file section. a/ and b/ are fixed prefixes (diff.noprefix, --no-prefix, diff.mnemonicPrefix change them).
Extended headers Optional lines: old mode/new mode, new file mode, deleted file mode, similarity index N%, rename from/rename to, copy from/copy to, dissimilarity index.
index old..new mode Abbreviated blob IDs before and after, and the mode when it did not change.
--- a/X, +++ b/Y The old and new file names; /dev/null stands for a file that does not exist on that side.
@@ -a,b +c,d @@ A hunk header: the old range starts at line a and spans b lines; the new range starts at c and spans d. A missing ,b means one line. Text after the second @@ is the nearest preceding "function line", chosen by diff.*.xfuncname or the built-in heuristics.
-line Present in the old file only.
+line Present in the new file only.
line Unchanged context (a leading space).
\ No newline at end of file The preceding line has no trailing newline.

By default there are three lines of context (-U<n> or diff.context changes this). When two changes are close together their hunks are merged into one.

Renames, copies and binary files

Rename and copy detection (-M, -C, on by default for git diff since diff.renames=true) shows similarity index and the two names, and the hunks only contain what changed between them. A pure rename has no hunks at all. For binary files the section says Binary files a/x and b/x differ, or with --binary contains a base85-encoded GIT binary patch that git apply can use.

Combined diffs

git show for a merge, git diff during a conflict, and git diff-tree -c or --cc print a combined diff. The header is diff --cc <file> (or diff --combined), the index line lists all parent blob IDs joined by commas, and each hunk starts with one more @ than the number of parents (@@@ -1,1 -1,1 +1,1 @@@ for two parents). Each line has one marker column per parent: a - in column one means the line was in parent 1 but not in the result; a + in column two means it appears in the result but not in parent 2. --cc ("compact combined") hides files and hunks where the merge simply took one side, so you see only real conflict resolutions.

Usage notes

  • Whitespace. -w, -b, --ignore-space-at-eol and --ignore-blank-lines change what counts as a change; --check reports whitespace errors. git apply --whitespace=fix can repair them.
  • Algorithms. --diff-algorithm=myers|minimal|patience|histogram choose different but equally valid diffs of the same files. --patience and --histogram often give more readable results on moved blocks.
  • Word and colour output. --word-diff, --color-words and --color-moved change the display only, not the file format.
  • Machine-readable. --raw prints one line per file with modes and IDs; --numstat and --name-status give compact tables. -z uses NUL separators for safe parsing.
  • Quoting. Unusual file names are written in quotes with C-style escapes unless core.quotePath is false.
  • Applying. git apply patch.diff checks and applies a diff to the working tree (--index also stages it, --check only tests it).
  • Order. Files are sorted by path. -O<orderfile> or diff.orderFile can move chosen files first.