Sub topic 7
Skipping formatting commits with ignore-revs files
This chapter assumes you can run git blame and read its output, and that you know how to use git log and read commit hashes. If blame is new to you, read the earlier chapters on blame output and -L first.
The problem: one commit owns every line
Teams often run a formatter such as Black, Prettier or gofmt over a whole codebase in a single commit. That commit changes the layout of thousands of lines and no behavior. After it lands, git blame credits the formatting commit and its author for every line it touched. The commit that explains why the code exists is hidden one step behind it.
The flags -w, -M and -C help with small whitespace and move noise. They don't cope well with a formatter that rewraps lines, changes quotes or reorders imports. For that case, Git can skip chosen commits entirely when it assigns blame.
How skipping works
When you tell blame to ignore a commit, Git does not attribute lines to it. For each line that the ignored commit changed, Git looks at the parent version and passes the blame back to the commit that last changed the matching line. The result reads as if the formatting commit had never happened.
Ignoring a commit on the command line
Use --ignore-rev with one full or abbreviated commit hash. You can repeat the option.
git blame --ignore-rev 8f3a2c1 src/app.py
git blame --ignore-rev 8f3a2c1 --ignore-rev 1b7d9e0 src/app.pyThis works for a one-off check. If you have several formatting commits, a file is easier to manage.
Using an ignore-revs file
An ignore-revs file lists one full commit hash per line. Blank lines are allowed, and a line starting with # is a comment. By convention the file is named .git-blame-ignore-revs and sits at the repository root.
# Reformat the codebase with Black
8f3a2c1d4e5b6a7980c1d2e3f4a5b6c7d8e9f012
# Switch to single quotes
1b7d9e04c5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0Pass the file with --ignore-revs-file:
git blame --ignore-revs-file .git-blame-ignore-revs src/app.pyTo make Git use the file every time, set the config option. Run it once per clone.
git config blame.ignoreRevsFile .git-blame-ignore-revsBecause the file is committed with the code, teammates only need that one git config command. Each person must run it in their own clone, since the setting is stored in local config. Some hosting sites, such as GitHub, read a file with this name automatically in their web blame view.
Hashes must be full and must survive history rewrites
The file needs full 40-character hashes. Abbreviated hashes in the file are not accepted the way they are on the command line. Copy them with git rev-parse or git log --format=%H:
git log --format='%H %s' -n 5Hashes are tied to history. If you rebase or squash the branch that contains the formatting commit, its hash changes and the entry no longer matches anything. Record the hash only after the commit is on a branch whose history will not be rewritten, such as the main branch.
Marking lines that were ignored
When blame skips a commit, a line may have no clear earlier origin, for example if the formatter created it from scratch. Git then still has to blame something. Two options show you when this guesswork happens:
git blame --ignore-revs-file .git-blame-ignore-revs \
--color-lines src/app.pyThe related config blame.markIgnoredLines puts a ? in front of lines whose blame was moved by an ignored commit. The config blame.markUnblamableLines puts * in front of lines that Git could not trace past an ignored commit. Treat both marks as a reminder to check those lines by hand.
git config blame.markIgnoredLines true
git config blame.markUnblamableLines trueCommon mistakes
- Using a short hash in the file. Use the full hash.
- Listing the wrong commit. A merge commit hash does not skip the formatting commit that came in through the merge. List the formatting commit itself.
- Ignoring a commit that changed behavior. Skipping a commit hides real changes. Only list commits that changed layout and nothing else.
- Expecting log searches to obey the file.
git log -Sand-Gsearch diffs and ignore this setting. It affects onlygit blame.
Exercise
Your team ran a formatter in commit 8f3a2c1d4e5b6a7980c1d2e3f4a5b6c7d8e9f012. Write the two commands that put this commit in an ignore-revs file at the repository root and make git blame use the file by default in your clone.
Show answer
echo 8f3a2c1d4e5b6a7980c1d2e3f4a5b6c7d8e9f012 >> .git-blame-ignore-revs git config blame.ignoreRevsFile .git-blame-ignore-revs
Exercise
After enabling the ignore-revs file, a line is still blamed to a commit you think is only formatting. What are two likely causes?
Show answer
The commit's full hash is not in the file (for example a short hash, or the hash changed after a rebase). Or the setting is not active in your clone because blame.ignoreRevsFile was never configured. A third possibility is that the line was created by that commit with no earlier origin, so Git has nothing to pass the blame to.