TIL: `git blame --ignore-rev` for ignoring formatting changes
When you want to introduce a formatter in an existing project, you have two extreme options - do a 'big bang' reformatting, or reformat only changed lines.
A big bang reformat is enticing because you get it done and can add a CI formatting check straight away. However, you would polute git blame with a bunch of no-value changes.
--ignore-revs to the rescue!
TIL that you can except commits out of git blame via --ignore-revs or --ignore-revs-file options.
# Changes from commit a228ad76 will be ignored
git blame pyproject.toml --ignore-rev a228ad76
GitHub respects a .git-blame-ignore-revs file that will do the same in GH blame views.
To provide a consistent experience to contributors, you can provide a project .gitconfig.
# .gitconfig
[blame]
ignore-revs-file = .git-blame-ignore-revs
When setting up the dev environment, you can choose to merge it with the users' gitconfig like so1, so all contributors get it for free.
# Get all include.path values from local git config
git config --local --get-all include.path \
# Check if .gitconfig is already included (literal match, no output)
| grep --quiet --fixed-strings '.gitconfig' \
# If not found, add it as an included config path
|| git config --local --add include.path '.gitconfig'
I first came across this when setting the all-green GitHub Action.