How to exclude commits from git blame (#tilPost)

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 messin…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

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...

Ignore commits via --ignore-rev

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.

Ignore commits via --ignore-revs-file

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 .git-blame-ignore-revs 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.

Add ignored revisions to your local Git config

To make things work in VS Code, I added the blame.ignoreRevsFile option to my local git config.

git config blame.ignoreRevsFile .git-blame-ignore-revs

This command added a new entry to your local .git/config 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!

Fun fact: GitHub picks up .git-blame-ignore-revs

And to close things: here's a little fun fact. If the file holding your ignored commits is named .git-blame-ignore-revs, 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!


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2022-08-13T22:00:00+00:00) How to exclude commits from git blame (#tilPost). Retrieved from https://www.scien.cx/2022/08/13/how-to-exclude-commits-from-git-blame-tilpost/

MLA
" » How to exclude commits from git blame (#tilPost)." Stefan Judis | Sciencx - Saturday August 13, 2022, https://www.scien.cx/2022/08/13/how-to-exclude-commits-from-git-blame-tilpost/
HARVARD
Stefan Judis | Sciencx Saturday August 13, 2022 » How to exclude commits from git blame (#tilPost)., viewed ,<https://www.scien.cx/2022/08/13/how-to-exclude-commits-from-git-blame-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » How to exclude commits from git blame (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/08/13/how-to-exclude-commits-from-git-blame-tilpost/
CHICAGO
" » How to exclude commits from git blame (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2022/08/13/how-to-exclude-commits-from-git-blame-tilpost/
IEEE
" » How to exclude commits from git blame (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2022/08/13/how-to-exclude-commits-from-git-blame-tilpost/. [Accessed: ]
rf:citation
» How to exclude commits from git blame (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2022/08/13/how-to-exclude-commits-from-git-blame-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.