How to exclude commits from git blame
- Published at
- Updated at
- Reading time
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
I created a
file (more on this file name later!)...
# Changed everything to tabs a926bba49c89a5b882cd298be3af2570b1e6252c
... and referenced the file when using
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
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
[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! 🎉
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!