Sub topic 11
Where blame and -S can mislead you
This chapter assumes you can read git blame output and run git log -S and -G. It covers the cases where those tools point you at the wrong commit, and how to check for each one.
Blame shows the last change, not the cause
git blame reports the most recent commit that touched each line. That commit is often not the one that introduced the behavior you care about. A typo fix, a rename or a reformat replaces the line, and blame now names that commit.
Check by blaming the parent of the commit that blame reported:
git blame -L 40,60 abc1234^ -- src/app.pyRepeat until the commit message explains the logic and not just a cosmetic edit.
Author and committer are different people
Every commit records an author (who wrote the change) and a committer (who applied it). They differ after git rebase, git cherry-pick, git commit --amend by someone else, and patches applied with git am. Blame prints the author by default, and the date it shows is the author date.
To see both, inspect the commit:
git show --format=fuller --no-patch abc1234The output has Author, AuthorDate, Commit and CommitDate lines. Rebased commits keep the original author date, so the date can be much earlier than the moment the code reached your branch.
Squash merges hide the real history
A squash merge combines a whole branch into one commit. Blame then names that single commit and its author for every line, even though several people wrote the lines over many commits. The commit message may only say "Merge feature X (#123)".
If your host links squash commits to pull requests, open the pull request. It usually lists the original commits and the discussion. If the branch was deleted, the review thread may be the only record left.
Bulk commits and moved code
Formatting commits, mass renames and license-header changes take over blame for every line they touch. Use --ignore-revs-file to skip them. Use -w, -M and -C to see through whitespace edits and moved code.
These options are heuristics. -M and -C detect moves by comparing text, so they can miss code that was changed while it was moved, and they can match unrelated lines that look alike. -C also takes longer on large repositories. Treat the output as a lead and confirm it with git show.
What `-S` can miss or over-report
git log -S'text' lists commits that changed the number of occurrences of text. This has three consequences:
- A commit that moves a line within one file, or edits around
textwhile keeping the same count, does not appear. - A commit that adds one occurrence and removes another in the same diff changes nothing in the count, so it is skipped.
- A short or common string matches many unrelated commits. Make the string specific, or restrict the search to a path.
If the count did not change but the line did, use -G, which matches added or removed lines. -G lists more commits, including ones that only reformat a matching line, so expect more noise.
git log -S'parse_config' --oneline -- src/
git log -G'parse_config' --oneline -- src/Comparing the two lists tells you which commits only touched the line without changing the count.
History is limited to what the repository holds
Several repository conditions truncate or distort results:
- A shallow clone (
git clone --depth) stops at its cutoff. Blame assigns older lines to the oldest available commit, which is often wrong. Rungit fetch --unshallowto get the full history. - Code imported from another repository without its history appears as one large commit.
- Rewritten history (force pushes,
filter-repo) changes commit hashes. Old hashes from tickets or chat logs may no longer exist. - Renames across files can break the trail unless you pass
-Cto blame or--followtogit logfor a single file.
A verification checklist
The flow below shows how to confirm a result before you rely on it.
- Read the commit message and the diff for the line, not just the hash.
- Check whether the commit is cosmetic, a squash merge or a bulk change.
- Confirm the repository is not a shallow clone.
- Cross-check with
git log -Sor-Gon the key string.
Exercise
You run git blame on a line and it names a commit "Squash merge feature-auth (#210)" with one author. Two colleagues say they wrote that line on the branch. What do you do to find the real author, and why might git log -S on the repository's main branch not help?
Show answer
Open pull request #210 (or the original branch commits if they still exist) to see the individual commits and their authors. git log -S on the main branch only sees the squash commit, which contains the whole change as one diff, so it names the same commit as blame.
Exercise
A search with git log -S'timeout' does not list a commit where someone changed timeout = 5 to timeout = 30. Why, and which option finds it?
Show answer
The number of occurrences of timeout is the same before and after the commit, so -S skips it. git log -G'timeout' finds it because it matches commits whose added or removed lines contain the pattern.