Menu

Search toolsChangelog

to move to openDescribe the problem, not the tool

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.

The left side of the comparison. CRLF and CR newlines are normalised to LF before lines are compared.

The right side. A changed line is represented as one deletion and one addition, as unified diff requires.

Unchanged lines shown around each edit. Nearby edits merge when their context overlaps.

Change summary
2 lines added, 2 lines removed
Lines added
2
Lines removed
2
Lines unchanged
2
Change hunks
1

Unified diff patch

notes.txt.patch · 98 B

A standard --- / +++ patch with @@ ranges, selected context and explicit missing-final-newline markers.

The complete line edit script

Unchanged11alpha
Removed2beta
Added2beta revised
Unchanged33gamma
Removed4delta
Added4epsilon
What to take away
  1. A changed line is encoded as a minus line followed by a plus line because unified diff has no separate replace operation.

  2. Unchanged alpha and gamma lines provide context that helps a patch consumer locate the edits in the target document.

  3. The --- and +++ headers use a cleaned local filename and contain no timestamp or machine path.

How it works

How it is done

  1. Normalise CRLF and lone CR line endings to LF, while recording whether each document's final logical line has a terminating newline.
  2. Build a longest-common-subsequence matrix over complete line text plus its termination state, choosing deletion before insertion when several minimum scripts tie.
  3. Expand every changed edit by the requested number of unchanged context lines and merge ranges that touch or overlap into unified diff hunks.
  4. 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.