fsylum.net

Ignoring Specific Commits From Git Blame

· 1 minutes read

Starting from version 2.23, Git added the ability in the config to specify a file where you can list out all the commits or revisions that should be ignored when you are running git blame.

This is particularly useful if you want to exclude specific commits that have no direct impact on the code, such as formatting changes. For example, in every new Laravel install that I have, I will always format the codebase against my own configuration of Laravel Pint and Rector.

To get started, create a file in our repository root. This can be anything, but common convention is to use .git-blame-ignore-revs as the file name.

This is one real-world example that I have in one of my projects.

# Initial file formatting changes via Pint
99d4ec50954062dec751c5fc080b193887824b38

# Initial file formatting changes via Rector
c719f81619374e43a66c4cfe80061acae6b1b1f7

You can add any number of revisions, but make sure to put the full SHA-1 hashes. Comment can be added, starting with # to help documenting what is being ignored.

The next step is to tell Git to use this file whenever you run git blame. This can be done by specifying the file via the blame.ignoreRevsFile config like this:

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

That’s it! From now on Git will automatically exclude those revisions when you run git blame.