Video in progress
In a big refactoring PR, code was moved from one place to another and reformatted. Which Git options do you use to make the diff easy to read?
--color-moved shows moved blocks in a separate color, -w hides whitespace changes, and --word-diff gives the difference inside a line word by word. First I look at the overall picture with --stat.
📖 Answer
In an ordinary diff, moving a 40-line method to another class shows up as 40 red and 40 green lines. The reviewer is forced to compare by eye to see whether anything inside changed.
git diff --stat origin/main...HEAD # which files and how much
git diff --color-moved=zebra \
--color-moved-ws=allow-indentation-change origin/main...HEAD
git diff -w origin/main...HEAD # hide only whitespace changes
git diff --word-diff origin/main...HEAD # exactly which word changed inside a line
With --color-moved, a block that moved unchanged comes out in a different color, and a line that changed inside stands out as ordinary red/green. allow-indentation-change recognizes a block as "moved" even if it was shifted in by one level.
A permanent setup:
git config --global diff.colorMoved zebra
git config --global diff.algorithm histogram
The histogram algorithm often gives a clearer diff on Java code: it does not get "lost" pairing up } and blank lines.
GitHub also has a "Hide whitespace" button (?w=1 in the URL).
But the biggest help is not a Git option but splitting the commits: if "move only", "formatting only" and "logic change" are in separate commits, the reviewer can look at each one quickly. You then add the formatting commit to .git-blame-ignore-revs.
Sign up to mark studied questions and save your progress.