brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: diff.algorithm histogram for diffs that read better

Diff
~/tips/tip-git-diff-algorithm-histogram

git diff --diff-algorithm=histogram

Switches Git's diff algorithm so moved or repetitive code can show up as cleaner hunks.

Sometimes a diff makes it look like half a file was rewritten when you only moved or added a function. The diff algorithm is a setting, and a different one can change how the hunks line up.

The default is myers. Git also accepts minimal, patience and histogram. The docs describe histogram as an extension of patience that supports low-occurrence common elements, so it anchors on lines that are rare in the file instead of lines like a closing brace. Try it for one command first:

git diff --diff-algorithm=histogram

Git rejects a name it does not know, so a typo is easy to spot:

error: option diff-algorithm accepts "myers", "minimal", "patience" and "histogram"

If you like the result, make it the default:

git config --global diff.algorithm histogram

The setting also applies to git log -p and git show. It changes how a diff is displayed, not what your commits contain, so nothing needs undoing.

Caveat: on a simple change, such as one function added to a short file, the algorithms usually print identical output, so do not expect a difference every time. It shows up in files with many repeated lines. Also, two people with different settings see slightly different diffs of the same change, which matters when you compare notes about a patch.

Further reading: git-config: diff.algorithm

More Diff tips diff -r -x .git -x '*.log' one two diff -y -W 100 old.conf new.conf diff -Naur original/ changed/ > fix.patch patch -p1 --dry-run < ../fix.patch diff --strip-trailing-cr windows.txt unix.txt All Diff tips (12) All topics

Link