Who's to Blame?

Neil Shephard
22 December 2022 12:00

Git blame shows who made changes to which line of code for a given point in its history. This is useful if you are struggling to understand changes to a section of code as you can potentially contact the author and gain some insight.

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

For more detailed information on the array of options refer to the official documentation or see git blame --help.

Alias

Some people don’t like the pejorative nature of the word blame. That’s ok though, with a tweak to your configuration its possible to use the alias praise or simply who instead.

# 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

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 .gitblame-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

Contact Us

For queries relating to collaborating with the RSE team on projects: rse@sheffield.ac.uk

Information and access to JADE II and Bede.

Join our mailing list so as to be notified when we advertise talks and workshops by subscribing to this Google Group.

Queries regarding free research computing support/guidance should be raised via our Code clinic or directed to the University IT helpdesk.