Sub topic 5
Looking further back: blaming at a parent commit
This chapter assumes you can read git blame output, limit it with -L, and read git log output. You should also know that every commit has a parent, the commit it was built on.
Why the first blame answer is often not the last
git blame shows the commit that last changed each line. That commit is not always the one you want. Suppose a line has a wrong timeout value. Blame points at a commit titled "Rename config keys". That commit only renamed a variable on the line. The person who chose the timeout value, and the reason, are in an earlier commit.
To find it, you ask blame the same question again, but as the repository looked just before the commit you found. That state is the commit's parent.
Blaming at the parent
Git lets you add a revision to a blame command. Blame then starts from that revision instead of the current one. The revision abc1234^ means "the first parent of commit abc1234".
git blame -L 40,60 abc1234^ -- src/app.pyThis reports who changed lines 40 to 60 of src/app.py as they were before abc1234 was applied. The -- separates the revision from the file path. It prevents Git from confusing the two when a file has the same name as a branch.
Two details matter:
- The line numbers refer to the file at
abc1234^, not the file today. If earlier commits added or removed lines above, the same code can sit at different numbers. abc1234^is the first parent. For a merge commit,abc1234^2is the second parent, which is usually the merged branch.
A worked investigation
Start with the current file.
git blame -L 42,44 src/app.pyExample output:
9f3c2a1 (Dana Ortiz 2026-03-11 42) timeout = config.get("request_timeout", 5)
9f3c2a1 (Dana Ortiz 2026-03-11 43) retries = config.get("max_retries", 3)
c04be77 (Sam Lee 2025-11-02 44) return Client(timeout, retries)Lines 42 and 43 come from 9f3c2a1. Look at what that commit did.
git show 9f3c2a1 -- src/app.pyIf the diff shows the lines were only renamed, for example from "timeout" to "request_timeout", the real origin is further back. Blame the parent:
git blame -L 42,44 9f3c2a1^ -- src/app.pyNow the output might show the lines under their old text:
1d8e5b0 (Priya Rao 2024-08-19 41) timeout = config.get("timeout", 5)
1d8e5b0 (Priya Rao 2024-08-19 42) retries = config.get("max_retries", 3)
c04be77 (Sam Lee 2025-11-02 43) return Client(timeout, retries)Notice the line numbers moved by one. The default value 5 came from 1d8e5b0. Run git show 1d8e5b0 to read the message and the surrounding change. If that commit is also only a rename or a move, repeat the step with 1d8e5b0^.
Stopping conditions
Repeat the loop until one of these happens:
- The commit's diff and message explain why the line has its current meaning.
- Blame reports the first commit of the file, so there is nothing earlier to find.
- The commit is a bulk change, such as a formatter run or an import. Later chapters cover
--ignore-revs-filefor these.
A related shortcut exists. The --ignore-rev option skips a commit during blame without you stepping through it manually. Parent blaming is the manual, general version. It works for any commit you decide is not the answer, including ones nobody listed in advance.
Common errors
- Using
abc1234instead ofabc1234^. Blaming at the commit itself returns the same answer you already have. - Reusing today's line numbers. After you move to an older revision, check the code on screen, not just the numbers. Adjust
-L, or use a function name such as-L :parse_config. - Forgetting
--. Without it, a path that looks like a revision can produce an "ambiguous argument" error. - Following the wrong parent of a merge. On a merge commit,
^is the branch you were on when you merged. Use^2to follow the merged-in work.
Exercise
A blame on line 80 of lib/db.py points to commit 7be21c9, which only reformatted the line. Write the command that shows who last changed that line before 7be21c9, and explain why line 80 might need a different number in the output.
Show answer
Run git blame -L 80,80 7be21c9^ -- lib/db.py. The ^ selects the parent, so blame reports the file as it was before the reformatting. The line number refers to the file at that older revision. If earlier commits added or removed lines above it, the same code can sit at a different number, so check that the code shown is the line you meant, and adjust -L if it is not.