C.W.K.
Stream
Lesson 02 of 05 · published

log -S and log -G: Search History

~20 min · log, search

Level 0Untracked Rookie
0 XP0/47 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Pickaxe — search across time, not just space

Grep searches the working tree — what exists right now. git log -S and git log -G, the "pickaxe" flags, search across all of history: which commits introduced or removed this string anywhere in any file. The first time you use this on a real "did we ever ship feature X?" or "when did this magic constant first appear?" question, it feels like cheating. Once you internalize that history is searchable, ad-hoc archaeology becomes a few-second answer.

-S "term" finds commits where the count of that exact term changed — i.e., the term was added or removed. Best for tracking specific identifiers, function names, environment variables, or magic strings. -G "regex" finds commits whose diff matches the regex anywhere in changed lines. Looser; good for patterns like "any commit that touched a JSON config." Both default to searching all of history; combine with -- <path> to scope to a directory or file.

The flag that completes the workflow is -p — show the actual diffs of matching commits inline. git log -p -S "OAUTH_ENABLED" shows you the commits where OAUTH_ENABLED entered the codebase, plus the surrounding context of what else changed at the same time. Add --all to search every branch, not just current; add --source to label which ref each match came from.

For the structural cousin: git log --grep="auth" searches commit messages, not diffs. Useful when you remember roughly what the commit said. --author="Pippa" filters by author. --since="2026-04-01" --until="2026-05-01" filters by date range. Pickaxe + grep + author + date filters layer naturally — find every commit by a specific author that touched a specific identifier in the last quarter, in one query.

Code

Pickaxe everyday queries·bash
# When did OAUTH_ENABLED enter or leave the codebase?
git log -p -S "OAUTH_ENABLED"

# Same, but restricted to backend/:
git log -p -S "OAUTH_ENABLED" -- backend/

# Regex form — any commit that touched a Postgres URL:
git log -p -G "postgres://[a-zA-Z0-9_:@.]+" --all

# Search across every branch and label the source:
git log -p --all --source -S "deprecatedFn"

# Show only the file names changed (no full patches):
git log --name-only -S "deprecatedFn"
Combine with grep, author, date filters·bash
# Commits whose message mentions "auth":
git log --grep="auth" --oneline

# By a specific author in a date range:
git log --author="Pippa" --since="2026-04-01" --until="2026-05-01" --oneline

# All filters together — Pippa-authored auth-related diffs in April:
git log --author="Pippa" --since="2026-04-01" --until="2026-05-01" \
        --grep="auth" -p -S "authenticate"

# Save query to alias:
git config --global alias.spelunk 'log -p --all --source --decorate'

External links

Exercise

On any project, pick a non-trivial identifier (function name, env var, configuration key) you can find in the current working tree. Run git log -p -S "<term>" and trace its first appearance, any later moves, and any removal. Then pick a regex pattern (e.g. an outdated API endpoint, a deprecated import path) and use -G. Note in writing one question you could answer with pickaxe in seconds that would have taken minutes by reading code.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.