Chapter 7.5 — Finding Lost Code — Pickaxe Search☕ 16 min read

Finding Lost Code — Pickaxe Search

Pickaxe = forensic investigation. Code mein kuch bhi gayab toh -S lagao, pakka milega.

01The Pickaxe — Git's Metal Detector

git log -S "text" is called the pickaxe search — it finds commits that ADDED or REMOVED a specific string from the code.

Unlike --grep which searches commit messages, -S searches the actual CODE DIFFS. A commit "refactor: clean up" might have deleted your function. --grep won't find it. -S WILL.

The name "pickaxe" comes from Git's early documentation — you're mining through history for specific ore (text).

-S detects when the NUMBER of occurrences of a string changed. If "functionName" appears 2 times before and 1 time after, -S finds that commit.

This is THE tool for forensic code investigation: who deleted this, when was this added, where did this variable go?

# The pickaxe: find commits that added/removed specific text
git log -S "functionName" --oneline
# e5f6g7h refactor: clean up auth module  ← deleted functionName
# abc123d4 feat: add authentication       ← added functionName

# See the actual changes
git log -S "functionName" -p
# Shows the diff of each commit that changed "functionName"

# Find when a function was DELETED
git log -S "functionName" --diff-filter=D --oneline
# Only shows commits where the string was removed

# Find when a function was ADDED
git log -S "functionName" --diff-filter=A --oneline
# Only shows commits where the string was introduced
02-S vs -G vs --grep — Three Search Tools

--grep="text" — searches COMMIT MESSAGES. "Which commits mention 'bug'?"

-S "text" — searches DIFFS for exact string. "Which commits changed the occurrence count of 'text'?" Matches whole string occurrences.

-G "regex" — searches DIFFS with regex pattern. "Which commits changed lines matching this pattern?" More flexible than -S.

Key difference: -S matches when the NUMBER of occurrences changes. -G matches when ANY line matching the regex changes.

If you rename getUser to fetchUser: -S "getUser" finds the removal, -S "fetchUser" finds the addition. -G "User" finds both in one search.

For most use cases, -S with a specific string is what you want. Use -G only when you need regex patterns.

# --grep: search commit messages
git log --grep="login" --oneline
# Finds: "feat: add login page", "fix: login crash"
# Does NOT find: code changes that mention "login" in diffs

# -S: search code diffs for string (occurrence count change)
git log -S "login" --oneline
# Finds: commits where "login" was added/removed from CODE
# Even if commit message doesn't mention "login"

# -G: search code diffs with regex
git log -G "get|fetch|load" --oneline
# Finds: commits that changed lines matching get/fetch/load

# Practical comparison:
# Commit: "refactor: rename user functions"
# Changed: getUser() → fetchUser()
git log --grep="getUser"  # might NOT find this commit
git log -S "getUser"     # WILL find this (getUser removed)
git log -G "User"        # WILL find this (lines with User changed)
03Finding Deleted Code — The #1 Use Case

The most common pickaxe use: "A function/class/variable disappeared. When? Who? Why?"

Step 1: git log -S "functionName" --oneline — find all commits that changed it.

Step 2: git log -S "functionName" --diff-filter=D --oneline — find the specific commit that deleted it.

Step 3: git show <hash> — read the commit message to understand WHY it was deleted.

Step 4: Decide if you need to recover it. If yes: git checkout <hash>~1 -- file.js gets the file before deletion.

This works for ANY text: function names, variable names, API endpoints, config keys, import statements.

💡 Pro Tip: Pickaxe search is case-sensitive by default. Use -i flag for case-insensitive: git log -S "login" -i. But beware: -i with -S can be slow on large repositories because Git must check more diffs. For most cases, exact case search is faster and more precise.
04Pickaxe Across Branches and Files

By default, -S only searches the current branch's history.

git log --all -S "text" — search ALL branches. Essential when code was deleted on a different branch.

git log -S "text" -- src/auth/ — search only in a specific directory. Faster for large repos.

git log -S "text" -- file.js — search only in a specific file.

Combine with --source to see WHICH branch or tag contains each commit: git log --all -S "text" --source --oneline.

git log -S "text" --since="2024-01-01" — limit to date range.

git log -S "text" --author="Alice" — only Alice's commits that changed this text.

# Search ALL branches
git log --all -S "deprecatedFunction" --oneline
# a1b2c3d (feature/cleanup) refactor: remove deprecated APIs
# e5f6g7h (main) feat: add new API
# Found it! It was deleted on the feature/cleanup branch

# See which branch has each commit
git log --all -S "deprecatedFunction" --source --oneline
# Source shows which ref (branch/tag) contains each commit

# Search only in a specific file
git log -S "API_KEY" -- config.js --oneline
# Only finds changes to API_KEY in config.js

# Search in a directory
git log -S "validateUser" -- src/auth/ --oneline

# Combine with date and author
git log -S "login" --since="2024-01-01" --author="Alice" --oneline

# The NUCLEAR pickaxe — find anything, anywhere
git log --all -S "mysteriousBug" -p --source
# Searches all branches, shows diffs, shows branch names
05Real-World Pickaxe Scenarios

Scenario 1: "Who removed the rate limiter?"git log -S "rateLimiter" --diff-filter=D

Scenario 2: "When was this API endpoint added?"git log -S "/api/users" --diff-filter=A

Scenario 3: "Where did this bug come from?"git log -S "knownBugPattern" -p → find when it was introduced

Scenario 4: "Which branch has the fix?"git log --all -S "fixForBug123" --source

Scenario 5: "Who changed this config value?"git log -S "max_connections = 100" --

The pickaxe is Git's most powerful forensic tool. Most developers only learn it after years of Git usage, and then wonder how they ever lived without it. Any time you need to know WHEN something changed in the code, regardless of commit messages, -S is your answer. It's the difference between guessing and knowing.

Lo kar liya — Key Points:

  • ✅ git log -S "text" (pickaxe) finds commits that added or removed specific text from code diffs
  • ✅ --grep searches commit messages; -S searches actual code changes — fundamentally different questions
  • ✅ Use --diff-filter=D with -S to find only deletions, --diff-filter=A for only additions
  • ✅ Search all branches with --all: git log --all -S "text" --source to find code on any branch
  • ✅ Combine with file paths: git log -S "text" -- file.js to search only in specific files
  • ✅ Pickaxe is case-sensitive by default — use -i for case-insensitive (may be slower on large repos)
  • ✅ The #1 use case: finding when code was deleted and understanding why — git show the resulting commit
Course Search
Search across all chapters & stages
📖

Search the course

Type any topic — branching, stash, rebase, hooks — and jump straight to that chapter.

merge branchesgit stashundo commitrebase