Generating a Unified Diff from Two Texts

Use the Generate Diff mode to create a unified diff from two versions of a text. Learn how the LCS algorithm computes minimal changes.

Common Use Cases

Detailed Explanation

Generating Unified Diffs

The Generate Diff mode takes two texts and produces a unified diff that can be shared, applied elsewhere, or stored for reference.

How to Use

  1. Switch to Generate Diff mode
  2. Paste the original text in the left panel
  3. Paste the modified text in the right panel
  4. Click Generate Diff
  5. Copy the resulting unified diff

Example

Original:

def hello():
    print("Hello, World!")

def main():
    hello()

Modified:

def hello(name="World"):
    print(f"Hello, {name}!")

def goodbye(name="World"):
    print(f"Goodbye, {name}!")

def main():
    hello("Alice")
    goodbye("Alice")

Generated Diff:

--- a/original
+++ b/modified
@@ -1,5 +1,9 @@
-def hello():
-    print("Hello, World!")
+def hello(name="World"):
+    print(f"Hello, {name}!")

+def goodbye(name="World"):
+    print(f"Goodbye, {name}!")
+
 def main():
-    hello()
+    hello("Alice")
+    goodbye("Alice")

The LCS Algorithm

The diff generator uses the Longest Common Subsequence (LCS) algorithm to compute the minimum set of changes:

  1. Build a dynamic programming table comparing all lines
  2. Trace back through the table to identify common lines
  3. Lines not in the LCS are either additions or removals
  4. Group changes into hunks with surrounding context lines

Context Lines

By default, the generated diff includes 3 lines of context around each change, matching the diff -u standard. Context lines help:

  • Identify the exact location of changes in the file
  • Enable fuzzy matching when applying the patch to a slightly different version
  • Provide human-readable context when reviewing the diff

Use Case

You made changes to a shared configuration file and need to send only the differences to your team. Generate a unified diff instead of sending the entire file — recipients can apply just your changes.

Try It — Diff Patch Applier

Open full tool