Skip to content

Instantly share code, notes, and snippets.

@gtirloni
Last active July 20, 2023 13:23
Show Gist options
  • Select an option

  • Save gtirloni/6af5ce40831a5d46d236a5e8629d2092 to your computer and use it in GitHub Desktop.

Select an option

Save gtirloni/6af5ce40831a5d46d236a5e8629d2092 to your computer and use it in GitHub Desktop.
git-blame ignore major formatting commits

git-blame shows what revision and author last modified each line of a file.

$ echo "this is a test" > test.txt
$ git add test.txt
$ git commit -m "first commit"
$ git blame test.txt
^d12054e (Giovanni Tirloni 2023-07-20 10:08:32 -0300 1) this is a test

If a codebase needs a major cosmetic change like fixing the formatting (e.g. terraform fmt -recursive, black, etc.), that commits is going to make tracking changes harder because it shadows all previous commits:

~/git-blame-demo $ vi test.txt 
~/git-blame-demo $ git add test.txt 
~/git-blame-demo $ git commit -m "second commit (format only, no functional changes)"
[main a7b7f21] second commit (format only, no functional changes)
 1 file changed, 1 insertion(+), 1 deletion(-)
 
~/git-blame-demo $ git blame test.txt
a7b7f214 (Giovanni Tirloni 2023-07-20 10:10:47 -0300 1) this  is  a  test

The last commit a7b7f214 (formatting only) and it's usually not very productive when you want to see what really changed in that file.

One way to overcome this situation is to tell git-blame to ignore certain commits. This is done through the configuration option config blame.ignoreRevsFile:

Add the commit SHA to a file that will hold all commits that are ignored by git-blame:

$ echo "a7b7f214539c875b6aa18b68f65186d07ae008fb" >> .gitblameignore
$ git config blame.ignoreRevsFile .gitblameignore

Notice that git-blame now ignores commit a7b7f214 and shows the previous one (d12054e):

$ git blame test.txt
^d12054e (Giovanni Tirloni 2023-07-20 10:08:32 -0300 1) this  is  a  test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment