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.
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
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.
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
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-eoland--ignore-blank-lineschange what counts as a change;--checkreports whitespace errors.git apply --whitespace=fixcan repair them. - Algorithms.
--diff-algorithm=myers|minimal|patience|histogramchoose different but equally valid diffs of the same files.--patienceand--histogramoften give more readable results on moved blocks. - Word and colour output.
--word-diff,--color-wordsand--color-movedchange the display only, not the file format. - Machine-readable.
--rawprints one line per file with modes and IDs;--numstatand--name-statusgive compact tables.-zuses NUL separators for safe parsing. - Quoting. Unusual file names are written in quotes with C-style escapes unless
core.quotePathis false. - Applying.
git apply patch.diffchecks and applies a diff to the working tree (--indexalso stages it,--checkonly tests it). - Order. Files are sorted by path.
-O<orderfile>ordiff.orderFilecan move chosen files first.