Lesson Completion
Back to course

Searching Content with Pickaxe

Intermediate
7 minutes4.8Git

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:

FlagWhat It SearchesWhen 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 diffFind 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

flowchart LR QUERY["🔍 What string?"] --> PICKAXE["git log -S string"] PICKAXE --> RESULT["📌 Commit abc123<br/>Author: Jane<br/>Date: Jan 15"] style QUERY fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style RESULT fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb

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

bash
# ─── 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 removed

Key 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 -p to see the actual diff, -- path to 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" --oneline finds commits where the function was added or removed. Add -p to see the diffs, confirming which commit introduced it.

  • Q: What is the difference between -S and -G in git log? A: -S finds commits where the number of occurrences of a string changes (added or removed). -G finds commits where the string appears anywhere in the diff. -S is more precise for "when was this introduced"; -G is 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.

Topics Covered

Git HistorySearch

Tags

#git#pickaxe#search#debugging

Last Updated

2026-02-13