Ignoring Revisions with Git Blame

May 15, 2022

Ovreview

git blame is a useful command to display the author metadata history of a file tracked by git. Runing git blame will output the Id, Authour, Timestamp, Line Number and Line Content of a file. For example:

^6e01b29 (Luke Miloszewski 2022-02-01 08:00:00 +0200  1) # README
6e01b297 (Luke Miloszewski 2022-02-01 08:00:00 +0200  2)
1d9978a7 (Luke Miloszewski 2022-02-01 08:15:00 +0200  3) ## Introduction
1d9978a7 (Luke Miloszewski 2022-02-01 08:15:00 +0200  4)
3w80fv3g (Luke Miloszewski 2022-02-01 08:30:00 +0200  5) Some random content.

Usage

  • view git blame:
git blame README.md
  • view blame over a range of lines:
git blame -L 1,5 README.md
  • view git blame and show user email address instead of username:
git blame -e README.md
  • view git blame and ignore whitespaces:
git blame -w README.md
  • view git blame and detect moved or copied lines within a file:
git blame -M README.md
  • view git blame and detect moved or copied lines from other files that were modified in the same commit:
git blame -C README.md

Ignoring Git Revisions

There are cases where you may want to ignore certain code changes made to a repository. For example, suppose someone makes a lot of formatting changes to a large codebase. The most recent git blame logs would be riddled with that particular author's metadata, despite not contributing to the underlying logic that made up those changes.

With the release of v2.23.0, git introduced the --ignore-rev flag which allows you to ignore revisions by passing in a commit revision.

git blame --ignore-rev 0ea9470g README.md

Furthermore, git introduced the --ignore-revs-file flag which allows you to configure which revisions to ignore in one central file (you will need to inlcude the complete revision hash).

This requires a .git-blame-ignore-revs file (usually in the root of the repository):

0ea9470guoem7a085302f26a51901e1962f47689

You can then reference this file when running the command:

git blame --ignore-revs-file .git-blame-ignore-revs README.md

Configuration

To avoid having to reference the --ignore-rev or --ignore-revs-file flags each time, you may configure this setting with git either locally or globally.

To configure locally:

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

To configure globally:

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

Git will throw an error if this setting is configured globally and a repository has no .git-blame-ignore-revs file. Furthermore, source control platforms like GitHub and GitLab may not support ignoring revisions.