Sub topic 4
Finding when a string appeared or disappeared with git log -S
This chapter assumes you can read git log output, run commands in a terminal, and know what a commit and a diff are. Regular expressions are not needed here.
git blame only works on lines that exist today. If a function was deleted, or a setting was removed, blame cannot point to it. The "pickaxe" search, git log -S, covers that case. It searches history instead of the current file.
What -S does
git log -S<string> lists commits that changed the number of occurrences of <string> in the repository. It compares the count before and after each commit's diff. If the count differs, the commit is listed.
git log -S'parse_config' --onelineTypical output:
9f3c2ab Remove legacy config loader
41d7e08 Add parse_config helperThe newest commit comes first. In this example, 41d7e08 added the string and 9f3c2ab removed it. You do not need the string to exist in the current files.
The -S option is attached directly to its argument, with no space. Quote the string so the shell does not change it.
Seeing the change
Add -p to print each matching commit's diff. Add a path to limit the search to part of the repository.
git log -S'parse_config' -p -- src/Read the diff to find the line that was added (+) or removed (-). The commit message and author tell you why and who. Searching a smaller path is also faster on large repositories.
A worked example
Suppose a bug report says the app no longer retries failed uploads. You remember a constant named MAX_RETRIES, but it is not in the code now. Blame cannot help.
- Search for the string:
``bash git log -S'MAX_RETRIES' --oneline ``
- Two commits appear: one that added it and one that removed it. Open the newer one:
``bash git show 7be21c4 ``
- The diff shows
MAX_RETRIESdeleted during a refactor of the upload module. The commit message does not mention retries. You now know when the behavior changed and who to ask.
What -S does not match
Because -S compares counts, some edits are invisible to it.
- A commit that moves a line within a file keeps the count the same, so it is skipped.
- A commit that changes one
parse_configcall into anotherparse_configcall also keeps the count the same. - A commit that adds one occurrence and removes another cancels out.
Chapter results can therefore look incomplete. If you expect a commit and it is missing, the count probably did not change. The next chapters cover -G, which matches on changed lines instead.
Choosing -S
Use -S when the question is "when did this string enter or leave the code?" Use git blame when the question is "who last touched this line that exists now?"
| Question | Tool |
|---|---|
| Who last changed this line? | `git blame` |
| When was this function removed? | `git log -S` |
| When was this string introduced? | `git log -S` |
Exercise
A config key cache_ttl was deleted from the project months ago. Which command lists the commits that added or removed it, and why can't git blame find the commit that deleted it?
Show answer
Run git log -S'cache_ttl' --oneline. Blame only annotates lines present in the file at the chosen revision, so a deleted line has nothing to attribute. The -S search looks at the diffs of past commits, so it finds the commit where the number of occurrences changed.
Exercise
A commit changes timeout = 5 to timeout = 10. Would git log -S'timeout' list it? Explain.
Show answer
No. The string timeout appears the same number of times before and after the commit, so the count does not change. A search on changed lines, such as -G, would list it.