Skip to content
Unified Diff Format
step 1/5

Reading — step 1 of 5

Read

~1 min readDiff Output

Unified Diff Format

The format git diff and most diff tools produce:

--- a/file.txt    2024-01-01 12:00:00
+++ b/file.txt    2024-01-02 12:00:00
@@ -10,6 +10,7 @@ optional_context
 unchanged line
 another unchanged
-removed line
+added line
+another added
 more unchanged
 last context

Header: file paths and timestamps.

Hunk header: @@ -<old_start>,<old_count> +<new_start>,<new_count> @@. Means: "lines old_start to old_start+old_count-1 in OLD became new_start to new_start+new_count-1 in NEW."

Each line:

  • Starts with (space): unchanged context
  • Starts with -: removed (old only)
  • Starts with +: added (new only)

Default context lines: 3 before and after each hunk. Adjacent hunks merge if their context overlaps.

Patch tools read this format to apply changes. patch -p1 < diff.patch applies a unified diff to a directory.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…