Sub topic 15
Worked example: tracing when and why a change happened
This chapter assumes you can read git log output and a unified diff, and that you know the range syntax A..B. It combines those skills on one question: when did a behavior enter the codebase, and why?
The scenario
Your application now crashes on startup if the config file is invalid. Before, it started with defaults. The check is the call validate(config) in load() in app.py. You want to know which commit added it and what the author intended.
The method has four steps: locate the commit, read the change, read the context, then check how it reached the main branch.
Step 1: locate the commit
Search for commits that changed how many times a string appears. Restrict the search to the file you care about.
git log -S"validate(config)" --oneline -- app.pyEach result is a commit where the count of validate(config) in app.py went up or down. Suppose the output is one line, 8c4d9e2 Reject invalid config at startup. If the code was reformatted rather than added, -S misses it. In that case use -G"validate\(config\)", which matches added or removed lines by regular expression.
Step 2: read the change
git show 8c4d9e2git show prints the full message, then the patch. Read the message first. It often states the reason, and it may name a ticket or a pull request. Then check the hunk. Look for lines starting with + and confirm that validate(config) was added here and not moved from elsewhere.
For a quick view of scope, add --stat. A one-line fix and a 40-file refactor need different levels of attention.
Step 3: read the context
A single commit rarely tells the whole story. See how the function evolved:
git log -L :load:app.pyThis prints each commit that touched load in app.py, with a patch limited to that function. You can see what came before validate and whether it was later changed.
If the file was renamed, -S and -L on the new path stop at the rename. Follow the file across it:
git log --follow --oneline -- app.pyUse --follow with one file at a time. It does not work on a directory.
Step 4: check how it landed
The commit may live on a feature branch that was merged later. Look at the main line only:
git log --first-parent --oneline mainEach merge appears as one entry. Find the merge that brought in 8c4d9e2. Its message usually names the branch. To see everything that branch contributed, compare it with its merge base:
git diff main...feature --statThe three-dot form compares feature against the common ancestor, so it shows only the branch's own work.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `-S` returns nothing | The string count did not change, or the path is wrong | Try `-G`, or drop the path |
| `-S` returns too many commits | The string is common | Add a path, or use `-L` |
| History stops at a recent commit | The file was renamed | Use `--follow` |
| The merge commit shows no patch | `git show` on a merge prints a combined diff | Inspect the merged branch's commits directly |
Exercise
A function parse_config in src/config.c seems to have gained a null check at some point. Which two commands would you run to find the commit that introduced it and then view the function's full history?
Show answer
Run git log -S"parse_config" --oneline -- src/ to find commits that changed the occurrences of the name, or use -G with the null-check text. Then run git log -L :parse_config:src/config.c to see each change to the function as a patch. Use git show <hash> on the commit you find to read its message.