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: git diff --color-moved shows code that only moved

Git
~/tips/tip-git-diff-color-moved

git diff --color-moved

Colors moved lines differently in a diff, so real edits stand out during a review.

Move a function to the bottom of a file and a plain git diff shows one big red block and one big green block. Then you get to read both and check, line by line, that nothing changed on the way. I think that’s a waste of a review, and one flag fixes it.

git diff --color-moved

Git now spots lines that were removed in one place and added in another, and paints them in their own colors instead of plain red and green. Without a mode it uses zebra: blocks of moved text of at least 20 alphanumeric characters get the moved colors, and a switch between two alternating colors marks where a new block starts. Lines that are really new or really gone keep the normal colors, so the edits you do need to read stand out.

My favourite mode is dimmed-zebra, which also dims the boring middle of each moved block and leaves its edges bright:

git diff --color-moved=dimmed-zebra

When code moved and also got re-indented, say because it went into an if block, add --color-moved-ws=allow-indentation-change so the whitespace change doesn’t break the move detection.

If you like it, make it the default for every diff:

git config --global diff.colorMoved zebra

The limit is that it’s color and nothing else. Send the output to a file or a tool that strips color and the information is gone. A line that moved and also changed one character shows up as a normal removal plus addition, and blocks shorter than the 20-character threshold aren’t caught at all, so a moved one-liner still looks like an edit.

Further reading: git-diff documentation, git-config documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link