{# canonical_base is the OWNING tenant's origin: all 16 Peasy domains serve the same catalogue, so a page rendered by a non-owner points its canonical at the owner instead of competing with it. Falls back to this site for static/self-owned pages. #}
🍋
Menu
How-To Beginner 1 min read 263 words

Text Diff and Merge: Understanding Algorithms and Tools

Understand how diff algorithms work, the differences between line-based and word-based diffs, and best practices for three-way merging. Essential knowledge for code review and collaborative editing.

Key Takeaways

  • Diff algorithms find the Longest Common Subsequence (LCS) between two texts, then report everything else as additions or deletions.
  • Three-way merge uses a common ancestor to resolve changes.
  • When conflicts occur, the merge tool marks both versions with conflict markers (`<<<<<<<`, `=======`, `>>>>>>>`).
  • Keep changes small and focused to minimize conflicts

How Diff Works

Diff algorithms find the Longest Common Subsequence (LCS) between two texts, then report everything else as additions or deletions. The classic Myers algorithm runs in O(ND) time where N is the total text length and D is the number of differences. For similar texts (small D), this is nearly linear.

Diff Granularity

Level Compares Best For
Line-based Whole lines Source code, configuration
Word-based Individual words Prose, documentation
Character-based Individual characters Short strings, titles
Semantic Meaningful units Code-aware refactoring

Three-Way Merge

Three-way merge uses a common ancestor to resolve changes. When both sides modify different regions, the merge is automatic. When both sides modify the same region, a conflict occurs. The ancestor helps distinguish 'Alice changed this' from 'Bob changed this' — without it, all differences appear as potential conflicts.

Merge Conflict Resolution

When conflicts occur, the merge tool marks both versions with conflict markers (<<<<<<<, =======, >>>>>>>). Resolution strategies include:

  • Accept ours/theirs: Choose one side entirely
  • Manual edit: Combine elements from both sides
  • Re-implement: Rewrite the section from scratch

Best Practices

  • Keep changes small and focused to minimize conflicts
  • Rebase frequently against the main branch
  • Use semantic diff tools for code (AST-based diffing)
  • Review word-level diffs for prose and documentation

Compare and merge texts with the Peasy diff tool — visualize changes at line, word, or character level with syntax highlighting.