🔀 Fix git diff –color-moved Silently Failing to Detect a Moved Block Because Reindentation Changed Every Line’s Leading Whitespace
git diff --color-moved highlights lines that were moved rather than changed by matching blocks of identical lines between the old and new side of a diff, and its default mode requires an exact character-for-character match, including leading whitespace. A block of code moved to a new location and reindented in the same commit — extremely common when wrapping a function in a new conditional or nesting it inside a class — no longer matches its old self character-for-character, so Git falls back to showing the whole thing as a plain deletion plus addition instead of a highlighted move, hiding the fact that a reviewer is actually looking at the same logic relocated, not new logic that needs line-by-line scrutiny.
🔎 The Problem
$ git diff --color-moved
-function calculateTotal(items) {
- return items.reduce((a, b) => a + b.price, 0);
-}
+ function calculateTotal(items) {
+ return items.reduce((a, b) => a + b.price, 0);
+ }
# Exact same logic, moved one nesting level deeper and reindented -
# shown as a plain removal + addition, NOT highlighted as a move
$ git diff --color-moved --color-moved-ws=allow-indentation-change
# Same two blocks now correctly highlighted in move-color, since the
# comparison ignores the leading-whitespace change from reindentation
✅ Fix: Compare Moved Blocks While Ignoring Whitespace Changes
- Add
--color-moved-ws=allow-indentation-changealongside--color-movedso Git’s move-detection compares lines after normalizing whitespace differences, exactly the case a reindented-and-moved block needs to be recognized correctly. - Set
diff.colorMovedWSin git config toallow-indentation-changeas a sensible default, rather than remembering to pass the flag by hand every time you review a refactor-heavy commit. - When reviewing a pull request in a web UI instead of the CLI, check whether that platform’s own move-detection or whitespace-hiding view is enabled, since the same exact-match limitation applies there independently of your local git config.
🚨 Why This Is Dangerous
- A reviewer who doesn’t realize color-moved missed a relocation ends up re-reviewing already-correct logic line by line as if it were newly written, wasting review time at best, or approving a diff without noticing that the “new” code silently dropped a line during the reindent that an actual move-aware diff would have made obvious by comparison.
git diff –color-moved only recognizes a moved block as the same lines you already approved once if every character, including the whitespace in front of it, still matches — reindent it in the same commit, and as far as the default diff is concerned, it’s a brand new block.


