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.

Revisions

  1. gtirloni revised this gist Jul 20, 2023. 2 changed files with 60 additions and 40 deletions.
    60 changes: 60 additions & 0 deletions git-blame-ignore.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    [git-blame](https://git-scm.com/docs/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.
    40 changes: 0 additions & 40 deletions notes.md
    Original file line number Diff line number Diff line change
    @@ -1,40 +0,0 @@
    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
    ```
  2. gtirloni created this gist Jul 20, 2023.
    40 changes: 40 additions & 0 deletions notes.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    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
    ```