Sub topic 6
Seeing through whitespace changes and moved code with -w, -M and -C
This chapter assumes you can read basic git blame output and limit it with -L, as covered in the earlier chapters. You should also know that blame attributes each line to the last commit that touched it.
Why plain blame points at the wrong commit
By default, blame treats any changed line as new. Suppose Alice wrote a check in 2022. In 2024 Bob wrapped it in a new if block, which indented it by four spaces. Plain blame now credits Bob for the line, even though he changed only its indentation.
c3f9a1e2 (Bob Lee 2024-05-14 11:02:10 +0000 41) if user.is_active:The same thing happens when code is moved to another place in the file, or to another file. The moved lines look new, so blame credits the person who moved them. Three options let blame look past this.
Ignoring whitespace with -w
The -w option ignores whitespace when comparing a line with its parent version. Lines that differ only in indentation, trailing spaces or tabs versus spaces keep their original commit.
git blame -w src/app.pyUse it first when you suspect a re-indent. It costs almost nothing and rarely hides useful information. It does not help if the commit also changed the text of the line.
Detecting moved lines with -M
The -M option detects lines that were moved or copied within the same file. If Bob reordered two functions, -M credits each line to the commit that first wrote it, not to Bob's reorder.
git blame -w -M src/app.pyGit only treats a block as moved if it contains enough alphanumeric characters. The default threshold is 20, so very short blocks such as a lone } are not tracked. You can change it, for example -M10, to match smaller blocks. A lower number finds more moves but also produces more false matches.
Detecting copies from other files with -C
The -C option looks for lines that were moved or copied from other files. By default it only considers files modified in the same commit as the line. This covers a common refactor, where a function is extracted from app.py into config.py.
git blame -w -M -C src/config.pyWhen blame follows a line into another file, it reports the original file name. Repeating the option widens the search:
| Option | Where blame looks for the original lines |
|---|---|
| `-C` | Other files modified in the same commit |
| `-C -C` | Also files from the commit that created the file |
| `-C -C -C` | Files from any commit in the history |
Wider searches are slower on large repositories. They also raise the chance of a false match, because common boilerplate can match unrelated code. The default minimum for -C is 40 alphanumeric characters, and you can set it with -C60, for example.
How the options fit together
The options stack. Each one removes one kind of noise, and you can add them in the order shown below.
If none of the options resolve the line, the commit may have changed the text for real. In that case, blame at its parent as described in the previous chapter.
A worked example
Say src/config.py contains a function that blame credits to a commit titled "Split app.py into modules". The commit message suggests the code came from elsewhere. Run blame with all three options on the lines in question:
git blame -w -M -C -L '/def parse_config/,+15' src/config.pyIf blame now shows an older commit and a different file name, such as src/app.py, the lines were copied or moved. The author shown is the person who wrote the code, not the person who split the file. Then check that older commit with git show to read its message and context.
Making it the default
You do not have to type the options each time. Many people keep an alias:
git config --global alias.bl 'blame -w -M -C'After that, git bl src/app.py runs blame with all three options. Keep the plain git blame in mind for cases where you want to see exactly who last touched each line, including the re-indent.
Exercise
A commit that only reformats a file with tabs instead of spaces shows up in blame for every line. Which option removes it, and which option would you add if the same commit also moved a helper function from utils.py into core.py?
Show answer
Use -w to ignore the tab and space changes. Add -C to follow the helper function from utils.py, because the move happened between files modified in the same commit. You would add -M only if lines had moved within a single file.