Thursday, April 25, 2024
HomeJavaScriptLearn how to exclude commits from git blame

Learn how to exclude commits from git blame


After ten years of being a vivid house consumer, I made a decision to maneuver all my tasks to tabs. 😲

I utilized this variation to this weblog’s repository, and an issue got here to gentle. For those who change the formatting of lots of of information, you are messing along with your Git historical past. git blame turns into fairly ineffective. Hundreds of strains can be marked with an unimportant formatting change.

VS Code blame showing many "move to tabs" commits

I began googling round to learn the way to disregard commits in Git and VS code…

Ignore commits by way of --ignore-rev

For those who use git blame on the command line, the --ignore-rev choice permits you to blame a file with out contemplating this nasty formatting commit.

git blame --ignore-rev a926bba49c index.js

That is nice however did not assist me as a result of I not often use git blame on the command line, and even when I’d, I would not wish to all the time consider this argument.

I simply need git blame to work.

Ignore commits by way of --ignore-revs-file

I continued the CLI journey. It’s also possible to outline a file to specify all commits that must be ignored by git blame with the --ignore-revs-file argument.

I created a .git-blame-ignore-revs file (extra on this file identify later!)…

# Modified every part to tabs
a926bba49c89a5b882cd298be3af2570b1e6252c

… and referenced the file when utilizing git blame:

git blame --ignore-revs-file .git-blame-ignore-revs index.js

This strategy works higher as a result of now I may add future commits to the revision file, and every time the ineffective commits stand in the way in which, I may use the --ignore-revs-file flag.

Nevertheless it’s not ultimate and it nonetheless did not work in VS Code.

Add ignored revisions to your native Git config

To make issues work in VS Code, I added the blame.ignoreRevsFile choice to my native git config.

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

This command added a brand new entry to your native .git/config file.

[blame]
    ignoreRevsFile = .git-blame-ignore-revs

On the command line, git blame then ignores the required commits robotically.

git blame index.js

And after restarting VS Code, it picked up the brand new Git config and did not present the formatting commit anymore! 🎉

Git blame in VS Code ignoring the formatting commit.

Sadly, the described Git config choice is barely an area one. It will not make it to a different machine. That is why I adjusted the git-blame-ignore-revs file with a small notice.

# Run this command to all the time ignore formatting commits in `git blame`
# git config blame.ignoreRevsFile .git-blame-ignore-revs

# Modified every part to tabs
a926bba49c89a5b882cd298be3af2570b1e6252c

There’s most likely some tooling to streamline this course of; when you have concepts, please let me know!

Enjoyable truth: GitHub picks up .git-blame-ignore-revs

And to shut issues: this is somewhat enjoyable truth. If the file holding your ignored commits is called .git-blame-ignore-revs, GitHub picks it up robotically.

GitHub blame interface telling that it's ignoring revisions due to a .git-blame-ignore-revs file.

And that is it! When you have ideas or concepts on how you can cope with ineffective commits, ship them my approach!



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments