Lessons · Git · who last changed this line
Who changed this line, and why
git blame file shows, for each line, the commit and author that last changed it. The hash leads to the message, which is where the why lives.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
The fastest route from a puzzling line to the reasoning behind it. Not for blame in the human sense: for context.
How to think about it
Blame the file, limit to the lines you care about with -L, then git show the hash. If the last change was a reformat, -w ignores whitespace and git log -L follows the line's real history.
Worked example
git blame a.txtEach line with the commit and author that last touched it.
git blame -L 3,5 a.txtOnly lines 3 to 5.
git show a1b2c3dThe commit blame named: the why, if the message was written well.
Your turn
Blame only lines 10 to 20.
git blame 10,20 app.py
Try it in a real repository
The trap
Blame names the last change, which is often a reformat or a rename. Use -w to skip whitespace changes and git log -L to find the real author.