Skip to content

Instantly share code, notes, and snippets.

@gtirloni
Last active July 20, 2023 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gtirloni/6af5ce40831a5d46d236a5e8629d2092 to your computer and use it in GitHub Desktop.
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 (John Doe 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:

$ vi test.txt  # change file in non-functional way (e.g. fix formatting only)
$ git add test.txt

$ 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 test.txt
a7b7f214 (John Doe 2023-07-20 10:10:47 -0300 1) this  is  a  test

git-blame shows 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  # Major formatting fix" >> .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 (John Doe 2023-07-20 10:08:32 -0300 1) this  is  a  test

Notice that the commit still shows in the commit log as usual:

$ git log ./test.txt
commit a7b7f214539c875b6aa18b68f65186d07ae008fb (HEAD -> main)
Author: John Doe <john.doe@example.com>
Date:   Thu Jul 20 10:10:47 2023 -0300

    second commit (format only, no functional changes)

commit d12054e565c4d1b3a46c0f5c49f4b6c3563ab1a2
Author: John Doe <john.doe@example.com>
Date:   Thu Jul 20 10:08:32 2023 -0300

    first commit

Since these major reformatting events are rare, it's doable to add the individual commits to the .gitblameignore file whenever needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment