Handling Long Lines in Git Diff Output
When working with files containing long lines, git diff output becomes hard to read — the diff spans beyond your terminal width, forcing you to scroll horizontally or losing context entirely. While you can’t force all files to use short lines, several strategies make diff output much more manageable.
Using Word Diff for Line-Level Changes
The --word-diff flag changes how git diff displays modifications. Instead of showing two complete lines (one deleted, one added), it highlights the specific words or segments that changed within each line:
git diff --word-diff
Output format:
[-deleted word-]{+added word+}
This is especially useful for configuration files, long strings, or sentences where only a few words changed. By default, it uses the format above, but you can also specify alternatives:
git diff --word-diff=color # Color-coded changes (default)
git diff --word-diff=plain # Plain text format
git diff --word-diff=porcelain # Machine-readable format
Line Wrapping with External Pagers
To force line wrapping in your diff output, override the default pager:
PAGER='less -S' git diff
The -S flag in less allows horizontal scrolling while keeping the full context visible. Alternatively, use fold to hard-wrap lines:
git diff | fold -w 80 -s
This wraps at 80 characters on word boundaries (use -w to set a different width).
Combining Approaches
For maximum readability with long lines, combine word diff with line wrapping:
PAGER='less -S' git diff --word-diff
Or use a custom pager that handles both:
git config core.pager 'less -S'
git diff --word-diff
Diff-So-Fancy for Enhanced Output
Modern alternatives like diff-so-fancy provide syntax highlighting and automatic line wrapping without extra configuration:
brew install diff-so-fancy # macOS
sudo apt install diff-so-fancy # Debian/Ubuntu
git config core.pager "diff-so-fancy | less -S"
git diff
Configuring Globally
If you frequently work with long lines, set your preferred diff format in your Git config:
git config --global core.pager 'less -S'
git config --global diff.wordDiff true
Then git diff will use word-diff mode and line-wrapping pager automatically.
Other Useful Flags
git diff --stat: Shows only a summary of changes (file names and change counts)git diff --unified=3: Adjusts context lines (default is 3); use fewer for dense diffsgit diff --color-words: Like--word-diff=colorbut uses a different algorithm
Each situation is different — experiment with these options to find what works best for your workflow.
