Git log, diff, show, and blame: Searching History by Content
Two dots are asymmetric, three dots compare against the merge base, -S counts occurrences while -G matches diffs. The filters that turn a slow search into a fast one.

Quick Summary
git log walks history and filters it; git diff compares two states; git show presents one object; git blame attributes lines. They share a revision grammar, and that grammar is where most confusion lives: A..B is "reachable from B but not A", A...B is "the symmetric difference from the merge base", and a bare path after -- limits the walk to commits touching it.
Content search has two flavours that are constantly mistaken for each other. -S (pickaxe) finds commits where the number of occurrences of a string changed — added or removed. -G finds commits where the diff matches a regex, whether or not the count changed. For "when was this constant introduced", -S is right; for "when did this pattern appear in any form", -G.
git blame costs a linear walk of the file's history, and it degrades in two common ways: it attributes moved code to the commit that moved it, and it follows renames only when asked. -C -C and --ignore-rev fix most of that.
Basic Syntax and Core Signatures
# Log shape
git log --oneline --graph --decorate --all
git log --oneline -n 20
git log --since="2 weeks ago" --until="yesterday" --author="Ryu" --no-merges
git log --first-parent main # mainline only, ignore merged branches
git log --stat --summary
git log --format="%h %ad %an %s" --date=short
git log --reverse --format=%H main..HEAD
# Filter by path and content
git log -- src/parser.ts # commits touching a file
git log --follow -- src/parser.ts # across renames (single path only)
git log -S"MAX_RETRIES" --oneline # pickaxe: count of the string changed
git log -G"retry[A-Z_]+" --oneline # regex in the diff
git log --grep="fix: retry" --oneline # message search (basic regex)
git log --grep="retry" --grep="timeout" --all-match
git log -L 40,60:src/parser.ts # history of a line range
git log -L :parseConfig:src/parser.ts # history of a function
# Diff and show
git diff # working tree vs index
git diff --staged # index vs HEAD
git diff main..feature # two commits
git diff main...feature # feature vs its merge base with main
git diff --stat --name-only
git diff --word-diff
git show 3f2a1b9 # commit with its diff
git show 3f2a1b9:src/parser.ts # file content at that commit
git show v2.4.0 --stat
# Blame
git blame src/parser.ts
git blame -L 40,60 src/parser.ts
git blame -w -C -C src/parser.ts # ignore whitespace, detect moves and copies
git blame --ignore-rev 9c1e2f4 src/parser.ts
git blame --ignore-revs-file .git-blame-ignore-revs src/parser.tsKey Comparison Table
| Syntax | Meaning | Typical use |
|---|---|---|
A..B | Commits reachable from B, not from A | "What is on my branch" |
A...B | Symmetric difference since the merge base | "What each side changed" |
A^ | First parent of A | Include a commit in a range |
HEAD@{2} | Where HEAD was two moves ago | Reflog-specific |
-- path | Limit to commits touching the path | File history |
:path after a commit | Read a blob at that revision | git show sha:file |
| Flag | Searches | Detects | Misses |
|---|---|---|---|
-S"str" | Change in occurrence count of str | Addition and removal | Refactors that keep the count identical |
-G"regex" | Diff lines matching the regex | Pattern appearing in changed lines | Old matches that were not touched |
-L a,b:file | History of a line range | Every change to those lines | Moved code after a rewrite |
git blame | Current line attribution | Authorship of each line | Authorship before a move without -C |
git log --follow | Renames of one path | Content preserved across rename | Multi-file renames, and it stops at the root |
Practical Edge Cases and Deep Dives
1. git log -- file after a rename returns nothing
Without --follow, a path filter starts at HEAD and walks commits where that path exists. Before the rename, the file had a different name, so the walk stops at the rename. --follow traces it back, with limits: it works for exactly one path, and it is slow on large repositories because it compares trees at each step.
git log --follow --name-status -- src/parser.ts2. -S versus -G on a refactor
The pickaxe counts occurrences. Renaming MAX_RETRIES to RETRY_BUDGET with the same value and the same number of uses gives a commit where -S"MAX_RETRIES" matches (the old string disappeared) — good. A commit that only changes the surrounding function without touching the identifier does not match -S at all, while -G on a nearby pattern does. Choose by the question: "when did this exact token appear or disappear" is -S; "which commits touched code shaped like this" is -G.
3. Blaming moved code
A formatter pass or a file split makes git blame attribute every line to the move, which destroys the useful signal. Fix it at blame time with copy/move detection, or record the noise commit and ignore it permanently:
git blame -C -C -M src/parser.ts
printf '# formatting and move-only commits\n9c1e2f4\n5d6e7f8\n' > .git-blame-ignore-revs
git config blame.ignoreRevsFile .git-blame-ignore-revsThe ignore file must be committed; every developer needs blame.ignoreRevsFile configured or GitHub's blame view (which honours the file automatically) will disagree with the terminal.
4. Diffing across a merge base
git diff main..feature compares the two tips, so it includes changes made on main since the branch started, presented as if the feature removed them. git diff main...feature compares the feature tip against the merge base, which is what a reviewer expects. This is the single most common source of "the diff shows changes I did not make" in pull requests and scripts.
git diff --stat main...HEAD
git diff main...HEAD -- src/5. --grep is a basic regex, and multiple greps
--grep uses basic regular expressions by default (-E for extended), and it matches anywhere in the message. Two --grep flags are ORed unless --all-match is given. Case sensitivity needs -i, and searching all branches needs --all — without it, only the current branch's ancestry is searched, which is why a commit that exists on a colleague's branch is "missing".
6. Performance on a large repository
Full-history walks are linear in commit count. --first-parent, path limiting, and time bounds turn a minute-long search into a second. -L is the expensive one: it walks until the file is created, and the rewrite-detection options multiply the work. For repeated queries, git rev-list with the same filters is cheaper when you only need hashes:
git rev-list --count --since="30 days ago" main
git rev-list --first-parent --count v2.3.0..HEADLine-range history. git log -L answers "how did these lines change"; git blame answers "who last touched this line". For a bug in a configuration parser, -L :parseConfig:src/parser.ts walks the function even as it moves in the file, which blame cannot do because blame has no notion of a function. For a release review, blame with --since narrows to recent authorship:
git blame --since="3.months" -w src/parser.ts | grep -v "^\^" | head -30
git log -L :parseConfig:src/parser.ts --oneline | head -20The caret prefix in blame output marks lines that predate the file's oldest recorded version — useful when a file was imported from another repository, because it tells you the history simply is not there.
Common Git Operation Mistakes
| Symptom | Cause | Fix |
|---|---|---|
log -- <path> stops at a rename | --follow missing | Add --follow, one path at a time |
| Diff shows coworkers' changes | .. used instead of ... | git diff main...HEAD |
| Commit exists but log cannot find it | Not on the current branch | --all, or check git branch --contains |
-S finds nothing after a rename of the value | Count did not change | -G with a regex, or -S on the new name |
| Blame points at a formatting commit | Move/format noise | -C -C, or --ignore-revs-file |
--grep misses a match | Basic regex syntax, or case | -E, -i, quote the pattern |
git show sha:file says "path not found" | Path relative to the repository root, or wrong revision | Use the root-relative path; check with git ls-tree |
| History search takes minutes | Unbounded walk | --first-parent, --since, path limits |
Try matching browser tools
All toolsSmall utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Git guides
All Git guidesGit config: Aliases, rerere, and Credential Helpers
Five config scopes decide one value at a time. Aliases are user-level shorthand, rerere records conflict resolutions, and credential helpers choose where your secret lives.
Git Hooks: pre-commit, commit-msg, and Husky Automation
Hooks live in .git/hooks, which a clone never receives, so core.hooksPath and Husky are how they travel. Exit codes abort the operation, and every hook is bypassable.
Git bisect: Binary Search, Automated Scripts, and Bug Hunting
Mark one bad and one good commit and Git halves the range each step. bisect run automates it through exit codes, and monotonicity is the assumption that must hold.
Git ignore: Patterns, Negation, and Untracking Cached Files
Patterns are relative and last match wins, but a negation cannot revive a file inside an excluded directory. Ignore rules never touch tracked files: rm --cached first.
More in Git
All Git guidesThe guides filed next to this one in Git, in library order.