File Headers in Unified Diff Format
Learn about the --- and +++ file headers, diff command line, and index line in unified diffs. Understand what each part means and when they are required.
Detailed Explanation
Unified Diff File Headers
A complete unified diff starts with header lines that identify the files being compared. While the Diff Patch Applier tool does not require these headers, understanding them helps when working with patches from Git and other tools.
Complete Header Structure
diff --git a/src/utils.js b/src/utils.js
index 3a4b5c6..7d8e9f0 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -1,5 +1,7 @@
Header Components
| Line | Purpose |
|---|---|
diff --git a/... b/... |
Identifies the diff command and files (Git-specific) |
index 3a4b5c6..7d8e9f0 100644 |
Git blob hashes and file mode |
--- a/src/utils.js |
Path to the original (old) file |
+++ b/src/utils.js |
Path to the modified (new) file |
Special File Names
/dev/null— indicates the file was created (--- /dev/null) or deleted (+++ /dev/null)- The
a/andb/prefixes are Git conventions for old and new versions
Minimal Required Format
For the Diff Patch Applier, only the hunk headers (@@) and diff lines are required:
@@ -1,3 +1,4 @@
line 1
line 2
+new line
line 3
The ---, +++, and diff lines are optional but recommended for clarity. When generating diffs with tools, they are always included.
Use Case
You are extracting a patch from a Git pull request that contains multiple file diffs. Understanding file headers helps you isolate the specific file's changes to apply them individually.