Sub topic 8
Searching with patterns: --pickaxe-regex and -G
This chapter assumes you can run git log and read its output, and that you know basic regular expressions (character classes, *, ., \s). You should also know how git log -S works: it lists commits that changed the number of occurrences of a string.
Why a plain -S is not always enough
git log -S'text' matches a fixed string. Two situations make that limiting:
- You do not know the exact text. You remember a shape, such as "a timeout set to some number".
- The commit edited a line but left the number of occurrences unchanged. For example,
timeout = 10becomestimeout = 30. The count oftimeoutis the same before and after, so-S'timeout'skips that commit.
Git has two options for these cases: --pickaxe-regex and -G.
Regular expressions with -S: --pickaxe-regex
Adding --pickaxe-regex makes git treat the -S argument as a regular expression instead of a fixed string. The rule is otherwise unchanged. A commit is listed when the number of matches changes between its parent and the commit.
git log -S'retry.*backoff' --pickaxe-regex --onelineThis lists commits that added or removed a match for retry.*backoff. If a commit moves a matching line from one place to another in the same file, the count stays the same and the commit is not listed.
Matching changed lines: -G
-G<regex> always takes a regular expression. It looks at the diff of each commit and lists the commit when any added or removed line matches the pattern. It does not compare counts.
git log -G'timeout\s*=\s*[0-9]+' --oneline -pThis finds the commit that changed timeout = 10 to timeout = 30. The diff has a removed line and an added line that both match, so the commit is listed. Adding -p prints the diff, which lets you see the old and new values.
Choosing between -S and -G
| Question | Option |
|---|---|
| When did this exact string first appear or disappear? | `-S'string'` |
| When did a pattern appear or disappear? | `-S'regex' --pickaxe-regex` |
| Which commits touched any line matching a pattern, including edits? | `-G'regex'` |
-S gives shorter, more precise lists because it ignores edits that keep the count the same. -G gives more candidates, including ones that only reformatted a matching line.
Common errors
- Forgetting
--pickaxe-regex. Without it,-S'retry.*backoff'searches for the literal charactersretry.*backoffand usually finds nothing. - Shell quoting. Wrap the pattern in single quotes so the shell does not expand
*or\s. In-G'timeout\s*=\s*[0-9]+'the backslashes must reach git unchanged. - Regex dialect. Git uses POSIX extended regular expressions for these options. Perl-only syntax such as
\dmay not work, so use[0-9]as in the example. - Too broad a pattern.
-G'timeout'on a large repository lists many commits. Narrow the search with a path, such as-- src/, or with a date range such as--since=2025-01-01. - Reading only the commit list. A listed commit may match for an unrelated reason. Read the diff with
-pbefore drawing conclusions.
Putting it together
Suppose a request now times out after 30 seconds and you want to know who changed it from 10.
- Run
git log -G'timeout\s*=\s*[0-9]+' --oneline -- src/. - Pick the candidates that look related by message and date.
- Re-run with
-pon those commits, or usegit show <hash>, to confirm the old and new values. - Note the author and date, and keep the hash for the next steps of your investigation.
Exercise
A commit changed retries = 3 to retries = 5 in src/net.py. Would git log -S'retries' --oneline list it? Which option would, and why?
Show answer
No. The word retries appears the same number of times before and after, so -S does not list the commit. git log -G'retries\s*=\s*[0-9]+' --oneline would list it, because the diff has a removed and an added line that match the pattern.
Exercise
You run git log -S'cache.*ttl' --oneline and get no output, though you know such code existed. What is the likely cause, and how do you fix the command?
Show answer
Without --pickaxe-regex, -S searches for the literal text cache.*ttl. Add the flag: git log -S'cache.*ttl' --pickaxe-regex --oneline.