Make a Unified Diff Patch From Two Texts
Paste the original on the left and the revision on the right, then save the patch. In the worked example, beta becomes beta revised and delta becomes epsilon, so the minimum line script has two deletions and two additions; with three context lines those nearby edits appear together in one @@ hunk.
Unified diff patch
A standard --- / +++ patch with @@ ranges, selected context and explicit missing-final-newline markers.
The complete line edit script
| Unchanged | 1 | 1 | alpha |
| Removed | 2 | beta | |
| Added | 2 | beta revised | |
| Unchanged | 3 | 3 | gamma |
| Removed | 4 | delta | |
| Added | 4 | epsilon |
A changed line is encoded as a minus line followed by a plus line because unified diff has no separate replace operation.
Unchanged alpha and gamma lines provide context that helps a patch consumer locate the edits in the target document.
The --- and +++ headers use a cleaned local filename and contain no timestamp or machine path.
How it works
How it is done
- Normalise CRLF and lone CR line endings to LF, while recording whether each document's final logical line has a terminating newline.
- Build a longest-common-subsequence matrix over complete line text plus its termination state, choosing deletion before insertion when several minimum scripts tie.
- Expand every changed edit by the requested number of unchanged context lines and merge ranges that touch or overlap into unified diff hunks.
- Write cleaned --- and +++ labels, exact @@ start,count ranges and space, minus or plus line prefixes into a downloadable patch.
What it assumes
- Comparison is line-based and exact after newline normalisation; whitespace, case and Unicode-normalisation differences are not ignored.
- Each side is limited to 2,000 characters, keeping the quadratic longest-common-subsequence matrix bounded in the browser.
- More than one minimum edit script can exist. Deletion-first ties make this output repeatable but not uniquely mathematically privileged.
- A syntactically formed patch can still fail against a third file whose surrounding content has changed; this tool does not apply or merge patches.
Common questions
Why does replacing two lines count as four changes?
The line edit model removes each old line and inserts each new line. Two replacements therefore produce two deletions plus two additions.
Can the generated patch expose my local path?
No. Only the final cleaned label is used; slash-separated directories, backslashes and control characters are discarded before headers are written.
Sources
The full method, worked example and every assumption behind this figure are on Text Diff Maker.