Sub topic 10
A workflow: from a suspicious line to the commit that explains it
This chapter assumes you can read git log output, know basic regular expressions, and have met git blame, git log -S, -G and -L in earlier chapters. Here you combine them into one repeatable routine.
The question decides the tool
Start by naming what you want to know. Each tool answers a different question.
| Question | Tool |
|---|---|
| Who last touched this line? | `git blame` |
| Who touched it before that? | `git blame` at the parent commit |
| When did this string appear or vanish? | `git log -S` |
| When did a line matching a pattern change at all? | `git log -G` |
| How did this function evolve? | `git log -L` |
Blame works on code that still exists in the file. The log searches also find code that has been deleted.
The workflow
Step 1: Blame only the lines you care about
Suppose a timeout in src/app.py looks wrong. Narrow blame to the function and let it see through whitespace edits and moves.
git blame -w -M -C -L '/def parse_config/,+20' src/app.pyNote the commit hash on the suspicious line. Ignore the author name for now. The message and diff matter more.
Step 2: Read the commit, not just the hash
git show abc1234Check three things: the subject line, the diff around your line, and whether the commit touches many unrelated files. A commit that touches hundreds of files is probably a formatter run or a rename. It does not explain your line.
Step 3: Step past noise
If the commit is a bulk change, record it in an ignore file. Use the full 40-character hash, one per line.
echo "abc1234def5678abc1234def5678abc1234def56" >> .git-blame-ignore-revs
git config blame.ignoreRevsFile .git-blame-ignore-revs
git blame -w -M -C -L '/def parse_config/,+20' src/app.pyIf the commit is a real change but not the original one, blame the parent instead.
git blame -L 40,60 abc1234^ -- src/app.pyLine numbers refer to the file as it was at that revision, so re-check the range. Repeat until the commit message explains the line.
Step 4: Search when blame cannot see the line
Sometimes the line you care about is gone, or blame points at a refactor. Search the history for the string instead.
git log -S'parse_config' --oneline -- src/-S lists commits that changed how many times the string occurs. The oldest hit is usually where it was added. The newest is usually where it was removed. Add -p to see the diff.
git log -S'parse_config' -p -- src/If a line was edited but the count stayed the same, -S misses it. Use -G with a pattern instead.
git log -G'timeout\s*=\s*[0-9]+' --oneline -p -- src/Step 5: Confirm with the function history
Once you have candidate commits, view the whole life of the function.
git log -L :parse_config:src/app.pyThis shows each change in order, so you can check that your candidate commit fits the story.
A worked trace
Imagine blame shows timeout = 5 was last changed in a commit titled "Apply black formatting". That is noise.
- Add its hash to
.git-blame-ignore-revsand rerun blame. - Blame now points to a commit titled "Move config code to app.py". Run blame at that commit's parent to see the original file.
- The older commit is "Reduce timeout after incident 212". The message explains the value.
If step 2 had shown nothing useful, git log -S'timeout = 5' -p would list every commit that added or removed that exact text.
Exercise
A line retries = 3 appears in blame under a commit named "Reformat with prettier". The previous value was different, and you want the real change. Name two ways to move past the formatting commit.
Show answer
Add the formatting commit's full hash to a file used with --ignore-revs-file (or set blame.ignoreRevsFile) and rerun blame. Alternatively, run git blame abc1234^ -- file with the formatting commit's hash to blame the file as it was just before that commit. Using -w can also help if the reformat only changed whitespace.
Exercise
You removed a function legacy_sync last month and now need the commit that deleted it. Which command finds it, and why not git blame?
Show answer
git log -S'legacy_sync' --oneline (add -p to see the diff). Blame only annotates lines that exist in a revision's file, so it cannot show deleted code at the current revision.