Searching history
Overview
Code you didn’t write — or wrote a year ago — is full of questions. Why is this number 0.15? When did this function appear? Who knows about this code? Git’s history holds the answers, and a handful of search commands get you to them in seconds. This lesson is your detective kit.
git blame: who last changed each line?
git blame cart.js
git blame -L 10,20 cart.js # only lines 10–20
git blame -L '/0.15/,+1' cart.js # the line matching a pattern9c8b7a6d (Chen 2026-11-20 14:02:11 +0530 6) return t * (1 - 0.15); // TODO: configurableEach line shows the commit, author, date and line number. Take the commit ID to git show 9c8b7a6d — the message and diff usually explain the “why”, and on GitHub the commit links to its pull request and the discussion around it.
blame shows only the last change to each line. If that change was just reformatting, it’s not the commit you want:
git blame -wignores whitespace changes,git blame -Cdetects lines moved or copied from other files,git blame <commit>^ -- cart.jsblames the version just before that commit, to see what came earlier,- a
.git-blame-ignore-revsfile listing big formatting commits makesblameskip them (git config blame.ignoreRevsFile .git-blame-ignore-revs; GitHub honours this file too).
Despite the name, blame is about finding context, not culprits.
The pickaxe: git log -S and -G
To find when a piece of code appeared or disappeared:
git log -S applyDiscount --oneline # commits that added or removed this text
git log -S applyDiscount -p # with the diffs-S finds commits where the number of occurrences of the text changed — so it finds the commit that introduced applyDiscount and the one that deleted it, but not commits that merely edited lines around it.
-G takes a regular expression and finds commits whose diff has added or removed lines matching it — including lines that were just modified:
git log -G "price\s*\*" --onelineCombine with paths and other filters: git log -S password --all -- config/ searches every branch for commits touching a password in config/.
git log -L: the story of one function
git log -L :applyDiscount:cart.js
git log -L 40,60:cart.js # or a line rangeGit finds the function (using language-aware rules for many languages), then shows every commit that changed it, with only the relevant part of each diff. It’s the fastest way to understand how a piece of logic evolved.
git grep: search the code
git grep -n TODO # line numbers
git grep -n "applyDiscount" v1.0.0 # in an older version, without checking it out
git grep -c TODO # count per file
git grep -e "price" --and -e "qty" # lines containing bothgit grep searches tracked files only — so it skips node_modules and build output — and it’s very fast. Searching a tag or commit is something plain grep can’t do.
Following renames
git log cart.js stops at the commit where the file got its current name. Add --follow to continue through renames:
git log --follow --oneline -- cart.js
git log --follow -p -- cart.js(--follow works for one file at a time.)
Searching messages and authors
From Viewing history:
git log --grep="discount" -i # commit messages
git log --author="chen" # by author
git log --since="2026-10-01" --until="2026-11-01" -- src/cart.jsPutting it together
A typical investigation:
git blame -L '/0.15/,+1' cart.js→ commit9c8b7a6.git show 9c8b7a6→ “Increase discount to 15 percent” by Chen, referencing issue #88.- On GitHub, issue #88 explains the business decision.
git log -L :applyDiscount:cart.jsshows the earlier 10% version and when it was added.
Now you know what the number means, who decided it, and why — and whether you can change it.
Git 2.52 also added git last-modified, which quickly shows the most recent commit to touch each file in a folder — the “last commit” column you see when browsing a repository on GitHub.
Before asking “why is this here?”, ask Git. The answer is usually one blame and one
git showaway.
Investigate a mysterious line
In the repository the script builds, a function applyDiscount exists in cart.js, and a magic number 0.15 appears in it. Answer with Git: (1) who last changed the line with 0.15, and in which commit? (2) In which commit was the text applyDiscount first added? (3) Show the full history of the applyDiscount function only. (4) Find every place TODO appears in the current code. (5) The file used to be called basket.js — show its history across the rename.
mkdir search && cd search && git init -qb main
c() { git add -A && GIT_AUTHOR_NAME="$1" GIT_AUTHOR_EMAIL="$1@example.com" git commit -qm "$2"; }
printf 'function total(items) {\n return 0;\n}\n' > basket.js; c asha "Add basket"
git mv basket.js cart.js; c asha "Rename basket to cart"
printf 'function total(items) {\n return 0;\n}\n\nfunction applyDiscount(t) {\n return t * 0.9; // TODO: configurable\n}\n' > cart.js; c ben "Add discounts"
printf 'function total(items) {\n return 0;\n}\n\nfunction applyDiscount(t) {\n return t * (1 - 0.15); // TODO: configurable\n}\n' > cart.js; c chen "Increase discount to 15 percent"
echo "// TODO: tests" > test.js; c asha "Add test stub"
# 1. Who last changed the 0.15 line?
# 2. When was applyDiscount first added?
# 3. History of the applyDiscount function
# 4. Where is TODO now?
# 5. History across the renamemkdir search && cd search && git init -qb main
c() { git add -A && GIT_AUTHOR_NAME="$1" GIT_AUTHOR_EMAIL="$1@example.com" git commit -qm "$2"; }
printf 'function total(items) {\n return 0;\n}\n' > basket.js; c asha "Add basket"
git mv basket.js cart.js; c asha "Rename basket to cart"
printf 'function total(items) {\n return 0;\n}\n\nfunction applyDiscount(t) {\n return t * 0.9; // TODO: configurable\n}\n' > cart.js; c ben "Add discounts"
printf 'function total(items) {\n return 0;\n}\n\nfunction applyDiscount(t) {\n return t * (1 - 0.15); // TODO: configurable\n}\n' > cart.js; c chen "Increase discount to 15 percent"
echo "// TODO: tests" > test.js; c asha "Add test stub"
# 1. Who last changed the 0.15 line?
git blame -L '/0.15/,+1' cart.js # chen, "Increase discount…" commit
git log -1 --format="%an: %s" "$(git blame -L '/0.15/,+1' --porcelain cart.js | head -1 | cut -d' ' -f1)"
# 2. When was applyDiscount first added? (the "pickaxe")
git log -S applyDiscount --format="%h %an %s" # Add discounts (ben)
# 3. History of the applyDiscount function
git log -L :applyDiscount:cart.js --format="%h %s"
# 4. Where is TODO now?
git grep -n TODO # cart.js:6 and test.js:1
# 5. History across the rename
git log --follow --format="%h %s" -- cart.js # includes "Add basket"Summary
git blameshows the commit and author that last changed each line;-Llimits it to a range.git log -S textfinds commits that added or removed text;-G regexfinds commits whose changes match a pattern.git log -L :function:fileshows the full history of one function.git grepsearches the tracked files, in any commit, much faster than plain grep.git log --followkeeps a file’s history across renames.