The Hook (The "Byte-Sized" Intro)
"When was this function added?" "Who removed this config line?" These questions are impossible with git log alone. That's where pickaxe (-S) comes in — it searches every diff in history and finds the exact commit that added or removed a specific string. It's archaeology for code.
📖 What is Pickaxe Search?
Pickaxe search (git log -S and git log -G) finds commits where a specific string was added or removed in the diff — not just mentioned in the commit message.
Conceptual Clarity
-S vs -G:
| Flag | What It Searches | When to Use |
|---|---|---|
-S "string" | Commits that change the COUNT of "string" | Find when code was added/removed |
-G "regex" | Commits with "regex" anywhere in the diff | Find any mention in changes |
Key difference: -S finds commits where the string appears MORE or FEWER times after the change. -G finds commits where the string appears anywhere in the diff lines.
Real-Life Analogy
Pickaxe is like a transcript search. Instead of scanning every page of a book, you search "when was this phrase first written?" and get the exact page and author.
Visual Architecture
Why It Matters
- Code archaeology: Find when any piece of code was introduced.
- Debugging: Track down when a broken line was added.
- Code review: Understand the history of a specific function or variable.
- Forensics: Trace the origin of security-sensitive code.
Code
# ─── Find when a function was added ───
git log -S "validateToken" --oneline
# Shows commits that added or removed "validateToken"
# ─── See the actual diff ───
git log -S "validateToken" -p
# Shows the full diff for each matching commit
# ─── Regex search in diffs ───
git log -G "TODO|FIXME" --oneline
# Finds commits with TODO or FIXME in the diff
# ─── Narrow by file ───
git log -S "API_KEY" -- src/config.js
# ─── Combine with author ───
git log -S "validateToken" --author="Jane" --oneline
# ─── Find when a line was removed ───
git log -S "deprecatedFunction" --oneline
# The last commit in the list is where it was removedKey Takeaways
-S "string"finds commits that add or remove the string (count change).-G "regex"finds commits with the string anywhere in the diff.- Combine with
-pto see the actual diff,-- pathto narrow by file. - Essential for code archaeology — finding when any code was introduced.
Interview Prep
-
Q: How do you find the commit that introduced a specific function? A:
git log -S "functionName" --onelinefinds commits where the function was added or removed. Add-pto see the diffs, confirming which commit introduced it. -
Q: What is the difference between
-Sand-Gin git log? A:-Sfinds commits where the number of occurrences of a string changes (added or removed).-Gfinds commits where the string appears anywhere in the diff.-Sis more precise for "when was this introduced";-Gis broader. -
Q: How would you find when a configuration value was removed? A:
git log -S "CONFIG_VALUE" --oneline -- config/. The results show commits that changed the count of that string. The most recent match is likely where it was removed.