Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~4 min readGit
#Git#History#Debugging#CLI
3D illustration of a commit timeline with a magnifier over a blame column

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

bash
# 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.ts

Key Comparison Table

SyntaxMeaningTypical use
A..BCommits reachable from B, not from A"What is on my branch"
A...BSymmetric difference since the merge base"What each side changed"
A^First parent of AInclude a commit in a range
HEAD@{2}Where HEAD was two moves agoReflog-specific
-- pathLimit to commits touching the pathFile history
:path after a commitRead a blob at that revisiongit show sha:file
FlagSearchesDetectsMisses
-S"str"Change in occurrence count of strAddition and removalRefactors that keep the count identical
-G"regex"Diff lines matching the regexPattern appearing in changed linesOld matches that were not touched
-L a,b:fileHistory of a line rangeEvery change to those linesMoved code after a rewrite
git blameCurrent line attributionAuthorship of each lineAuthorship before a move without -C
git log --followRenames of one pathContent preserved across renameMulti-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.

bash
git log --follow --name-status -- src/parser.ts

2. -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:

bash
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-revs

The 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.

bash
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:

bash
git rev-list --count --since="30 days ago" main git rev-list --first-parent --count v2.3.0..HEAD

Line-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:

bash
git blame --since="3.months" -w src/parser.ts | grep -v "^\^" | head -30 git log -L :parseConfig:src/parser.ts --oneline | head -20

The 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

SymptomCauseFix
log -- <path> stops at a rename--follow missingAdd --follow, one path at a time
Diff shows coworkers' changes.. used instead of ...git diff main...HEAD
Commit exists but log cannot find itNot on the current branch--all, or check git branch --contains
-S finds nothing after a rename of the valueCount did not change-G with a regex, or -S on the new name
Blame points at a formatting commitMove/format noise-C -C, or --ignore-revs-file
--grep misses a matchBasic 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 revisionUse the root-relative path; check with git ls-tree
History search takes minutesUnbounded walk--first-parent, --since, path limits

Try matching browser tools

All tools

Small utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Git guides

More in Git

All Git guides

The guides filed next to this one in Git, in library order.