Configure Word-Level Diff for Prose
Set up .gitattributes and .gitconfig for word-level diffs on Markdown, text, and documentation files to see exactly which words changed.
Detailed Explanation
Word-Level Diffs for Documentation
Standard line-level diffs are designed for code where each line is a distinct statement. For prose — Markdown, plain text, LaTeX — a single word change in a paragraph shows the entire paragraph as modified. Word-level diffs solve this by highlighting individual word changes.
Setting Up in .gitattributes
*.md text diff=markdown
*.txt text diff=text
*.tex text diff=tex
*.rst text diff=rst
*.adoc text diff=asciidoc
The diff=markdown driver tells Git to use Markdown-aware hunk headers (showing the nearest heading), and enables better word-boundary detection.
Using git diff --word-diff
With the attributes set, you can use:
# Color-coded word diff
git diff --word-diff
# Porcelain format (for scripts)
git diff --word-diff=porcelain
# Plain format with delimiters
git diff --word-diff=plain
Output Example
Standard diff:
-The quick brown fox jumps over the lazy dog.
+The quick red fox leaps over the lazy cat.
Word diff:
The quick [-brown-]{+red+} fox [-jumps-]{+leaps+} over the lazy [-dog.-]{+cat.+}
Custom Word Regex
You can define what constitutes a "word" in .gitconfig:
[diff "markdown"]
wordRegex = [[:alnum:]_]+|[^[:space:]]
This treats each alphanumeric sequence and each non-space character as a word, producing finer-grained diffs for Markdown.
Automatic Word Diff for Specific Files
To always show word-level diffs for certain files without the --word-diff flag, add to .gitconfig:
[diff "prose"]
wordRegex = [[:alnum:]_]+|[^[:space:]]
command = "git diff --word-diff"
Then in .gitattributes:
*.md diff=prose
Use Case
Documentation-heavy repositories, technical writing projects, and open source projects with README files benefit from word-level diffs. When reviewing prose changes in pull requests, word diffs make it clear exactly which words were added, removed, or changed.