Git blame is a tool that allows you to find out who changed a line last, but it has its limitations. Some changes are only cosmetic, and you would rather ignore them.
Good news: You can ignore commits using the blame method. Using a file, I will show you a technique you can apply per project.
Important: This technique works per device, so it is optional between users!
We will do three things:
- Create a blame ignore file in the project.
- Add commit hashes to the ignore file.
- Instruct Git to ignore commits in the file.
Project file
You can name the blame ignore file however you like, but I recommend .git-blame-ignore-revs
to stay in line with other people's StackOverflow answers and blog posts.
Post-publication note: Another reason to use the standard name is that GitHub supports this filename by default. Hat tip to Chris Rose for this one.
Let us create this file in the project root directory.
On macOS and Linux:
touch .git-blame-ignore-revs
Using Windows command prompt:
type nul > .git-blame-ignore-revs
File contents
All you need to add to the blame files are commit hashes; that's it!
You can optionally add extra information using comments starting with #
:
# This is a comment.
# Newline below; all fine.
# Pretend this is a commit hash:
1a2b3c
Git configuration
The project file is useless until you instruct Git to use it for blame ignores, so let's do that.
You will have to update your Git settings, and it is up to you whether you change your global or local project settings.
To update only the current project's settings:
# Run this command from the project root:
git config blame.ignoreRevsFile .git-blame-ignore-revs
To update your global projects:
git config --global blame.ignoreRevsFile .git-blame-ignore-revs
These commands add the following configuration to your Git config file:
[blame]
ignoreRevsFile = .git-blame-ignore-revs
If you want to edit a config file manually, you can find its location using the following command:
git config --list --show-origin
Wrap-up
In a few minutes, you can improve your Git blames by letting them ignore specific commits without breaking everyone else's experience.
Ignoring commits can be risky, as blame will stop acknowledging part of a repository's history, so use this technique carefully!
Appendix: Example
I used blame ignores in my website repo multiple times to hide code formatting commits. In particular, I used Prettier to format JavaScript and converted indentations from spaces to tabs (more on that in the future).
I also like to hardcode the date and commit messages above the hashes since you can read those anywhere.
This is what the entire .git-blame-ignore-revs
looks like now:
# Run this command to ignore commits in `git blame`:
# git config blame.ignoreRevsFile .git-blame-ignore-revs
# 2024-08-09; Replace space indents with tabs in HTML
947eb6252e3ee4f9af6bfaab2c41440dee7f7753
# 2024-07-17; Replace tox space indents with tabs
0f3c1353ac01f1e6e376eb0ce60974935fcbbdf2
# 2024-07-10; Convert spaces to tabs using Ruff and Prettier
a2a5f200e9f940b9c78a4af50de1c3281188e4aa
# 2024-07-10; Run Prettier
0a6adacd7f7781b5f31ccc29ae22cdf44edb396a