Sub topic 10
Finding when code changed: -S, -G, and -L
You already know how to filter git log by date, author, and path. Those filters answer "who" and "when". This chapter covers the question "which commit touched this code?" It assumes you can read a unified diff and use git log -p.
Git has three tools for this. They search the content of changes instead of commit metadata.
Searching by string count with -S
-S is called the pickaxe. It finds commits that changed the number of occurrences of a string in a file.
git log -S"parse_config" --oneline -- src/A commit matches only if the count of parse_config differs between the commit and its parent. A commit that adds the first call, deletes the last one, or adds a new definition will match. A commit that moves a line containing parse_config from one place to another leaves the count unchanged, so it does not match.
This makes -S good for answering "when was this name introduced?" and "when was it removed?". Add -p to see the patch for each match.
Searching by pattern with -G
-G takes a regular expression. It matches commits whose diff has an added or removed line that matches the pattern.
git log -G"parse_config\(.*\)" --oneline -- src/Unlike -S, -G does not care about counts. A commit that edits a line containing parse_config matches, and so does one that moves such a line. The cost is more matches, so you read more output.
| Option | Matches on | Argument | Catches moved lines |
|---|---|---|---|
| `-S` | Change in occurrence count | Plain string | No |
| `-G` | Added or removed line matching the pattern | Regular expression | Yes |
Following a function with -L
-L shows the history of a line range or a function. Git extracts just the relevant lines from each commit's patch.
git log -L :parse_config:src/config.cThe :parse_config: form tells Git to find the function by name. Git uses the file type to decide where the function starts and ends. You can also give line numbers, such as -L 10,25:src/config.c.
You do not need to add -p. -L prints a patch for each commit that changed the range, and it follows the range as lines shift.
Choosing the right tool
Common errors
- Searching the whole repository. Without a path, these options scan every file in every commit and run slowly. Add
-- src/to narrow them. - Expecting
-Sto find edits. If a line changed but the count of the string stayed the same, use-G. - Wrong function name with
-L. If Git reports it cannot find the function, check the spelling. Use a line range if the file type has no function detection. - Forgetting
-pwith-Sand-G. They list commits only. Add-pto see the lines that matched.
Exercise
You want to know which commit first introduced a call to retry_request in src/. A teammate says a later commit only moved that line to another file. Which option do you use, and why does it ignore the move?
Show answer
Use git log -S"retry_request" --oneline -- src/. -S matches only commits where the number of occurrences of the string changes. A move that keeps the count the same does not match, so the commit that introduced the call stands out. If -S also listed the move commit, the move would have to change the count in a file you searched.
Exercise
Write a command that shows every change to the function load in app.py, with a patch for each commit.
Show answer
git log -L :load:app.py. -L prints a patch for each commit that changed the function, so -p is not needed.