Git blame shows who made changes to which line of code for a given point in its history.
Usage
Git blame works on individual files and so requires a filename, there are a host of options, for example -e
prints the authors email address -w
ignores changes to white space and -L 10,20
restricts output to the specified line range. If you want a the blame for a specific revision then you must include the hash.
git blame -e -w -L 10,20 f923la git_blame.org
Alias
Some people don’t like the pejorative nature of the word blame
. That’s ok though, with a tweak to our configuration its possible to use the alias praise
or simply who
.
# blame alias
git config --global alias.praise blame
git praise -L1,30 git_blame.org
# who alias
git config --global alias.who blame
git who -L1,30 git_blame.org
For more detailed information on the array of options refer to the official documentation or see git blame --help
.
Ignoring blame
Sometimes the case arises where you want to ignore blame. Perhaps the most common example is when an existing code base has been linted to conform to a particular style guide. Looking at who performed these changes is not informative and masks who made the changes and why. Its possible to ignore specific commits on the command line with --ignore-revs <hash> <file>
, but it will quickly become tedious to remember to ignore all blame across multiple commits. Fortunately you can save the commits to ignore to the file .git-blame-ignore-revs
(along with comments) so that they are stored. The full commit (40 characters) of hashes should be used.
# PEP8 compliance for module X
c00177a6121f86c001f338feff3280fd576fdbf3
# PEP8 compliance for module Y
db27fa5f18299ca631efc430512a3f358c2b154f
Now that you have the revisions in place to be ignored when reporting blame you can choose not to use it.
git blame --ignore-revs-file .git-blame-ignore-revs git_blame.org
…but this is tedious to remember to have to do each time and ideally others on your team should use this file too. You can configure Git to use this file by modifying the local configuration. Make sure to add it to your repository so that others can use it.
git config blame.ignoreRevsFile .git-blame-ignore-revs
git add .git-blame-ignore-revs
As of 2022-03-08 GitHub will also ignore commits in the blame view that are listed in .git-blame-ignore-revs
providing this file is in the root of your project folder.
Links
General
Resources
Reuse
Citation
@online{shephard2022,
author = {Shephard, Neil},
title = {Who’s to {Blame}},
date = {2022-12-17},
url = {https://blog.nshephard.dev/posts/whos_to_blame/},
langid = {en}
}