How to exclude commits from git blame
- Published at
- Updated at
- Reading time
- 3min
After ten years of being a vivid space user, I decided to move all my projects to tabs. ๐ฒ
I applied this change to this blog's repository, and a problem came to light. If you change the formatting of hundreds of files, you're messing with your Git history. git blame
becomes pretty useless. Thousands of lines will be marked with an unimportant formatting change.
I started googling around to find out how to ignore commits in Git and VS code...
If you use git blame
on the command line, the --ignore-rev
option lets you blame a file without considering this nasty formatting commit.
git blame --ignore-rev a926bba49c index.js
This is great but didn't help me because I rarely use git blame
on the command line, and even if I would, I wouldn't want to always think of this argument.
I just want git blame
to work.
I continued the CLI journey. You can also define a file to specify all commits that should be ignored by git blame
with the --ignore-revs-file
argument.
I created a
file (more on this file name later!)...
# Changed everything to tabs
a926bba49c89a5b882cd298be3af2570b1e6252c
... and referenced the file when using git blame
:
git blame --ignore-revs-file .git-blame-ignore-revs index.js
This approach works better because now I could add future commits to the revision file, and whenever the useless commits stand in the way, I could use the --ignore-revs-file
flag.
But it's not ideal and it still didn't work in VS Code.
To make things work in VS Code, I added the blame
option to my local git config.
git config blame.ignoreRevsFile .git-blame-ignore-revs
This command added a new entry to your local
file.
[blame]
ignoreRevsFile = .git-blame-ignore-revs
On the command line, git blame
then ignores the specified commits automatically.
git blame index.js
And after restarting VS Code, it picked up the new Git config and didn't show the formatting commit anymore! ๐
Unfortunately, the described Git config option is only a local one. It won't make it to another machine. That's why I adjusted the git-blame-ignore-revs
file with a small note.
# Run this command to always ignore formatting commits in `git blame`
# git config blame.ignoreRevsFile .git-blame-ignore-revs
# Changed everything to tabs
a926bba49c89a5b882cd298be3af2570b1e6252c
There's probably some tooling to streamline this process; if you have ideas, please let me know!
And to close things: here's a little fun fact. If the file holding your ignored commits is named
, GitHub picks it up automatically.
And that's it! If you have tips or ideas on how to deal with useless commits, send them my way!
Join 5.5k readers and learn something new every week with Web Weekly.