Code reviews can be very time-consuming. Especially if you are reviewing a lot of code changes. Often, code changes are simple refactorings, such as extracting code into methods/functions or moving code around. These changes are more or less irrelevant for the review, since they usually don’t change the behavior of the code. As a reviewer, one still wants to make sure that code hasn’t been changed during the code move.
This is where git colored move diffs come into play. They make it easier to detect and display code moves in a different color than other code changes.
Colored Move Diffs work with all git commends that display diffs, such as
git log -p, etc.
git diff --color-moved
You can also set different color schemes for moved code, I prefer the
dimmed-zebra color profile since it greys out moved code:
git diff --color-moved=dimmed-zebra
Here’s an example screenshot of how a code move looks like with git colored move diffs and
dimmed-zebra setting. The moved code is grey, so you can easily spot it the real change,
% 5 changed to
% 8, which might have been accidentally introduced during the code move (which I consider harmful since it mixes a refactoring and a behavioral change into one commit - but that’s a different story):
There is also an option to ignore whitespace changes in moved code:
git diff --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
To globally configure git to use colored moves in git diffs, run the following commands:
git config --global diff.colorMoved dimmed-zebra # enables colored moved diffs with the dimmed-zebra color profile
And now, happy code reviewing!