A unified diff is the patch format git and the patch tool understand: it records exactly which lines were removed and added, wrapped in hunks with location headers and a few lines of surrounding context. This generator diffs two pasted texts and emits a clean .patch file you can share or apply offline.
When to use a patch file
A patch file is useful any time you need to share a precise text change that can be applied exactly — a code fix that needs to be applied to many files, a configuration change to distribute to a team, a contribution to a project you cannot push to directly, or a change you want to store alongside a file rather than in its git history. The .patch format is also the standard for email-based patch workflows in open-source projects, where contributors send patches to a maintainer who applies them with git am or git apply.
How it works
- Both inputs are split into lines.
- A longest-common-subsequence (LCS) diff finds the minimal edit script — the smallest set of line deletions (
-) and insertions (+) that turns the original into the modified text; matching lines become unchanged context (). - Consecutive changes are grouped into hunks, each padded with up to N lines of context (default 3, matching git’s default) and separated into distinct hunks when the gap between change regions exceeds 2×N lines.
- Each hunk gets an
@@ -start,len +start,len @@header locating it in both the old and new file, and the whole patch opens with--- a/<name>and+++ b/<name>headers.
Reading a hunk header
@@ -12,7 +12,7 @@
This means: starting at line 12 in the original file, 7 lines are shown (6 context + 1 removed); in the new file, also starting at line 12, 7 lines are shown (6 context + 1 added). When a change inserts or deletes lines, the two numbers in the header differ.
Worked example
An original file contains .btn { color: red; }. Changing red to blue with 1 context line:
@@ -1,3 +1,3 @@
.btn {
- color: red;
+ color: blue;
}
The space-prefixed line is unchanged context. The - line was removed, the + line added.
Applying the patch
Save the output as fix.patch, then:
git apply fix.patch # inside a git repo
patch -p1 < fix.patch # with the patch utility (GNU patch)
The -p1 strips the leading a/ from file paths, which is what git-format patches use. Line numbers in the hunk header are 1-based, and lengths count context plus changed lines — both exactly matching what git diff emits. Trailing-newline differences are handled so the patch applies cleanly. Everything is computed locally; your text never leaves the browser.