Searching History: Grep, -S and -L
Searching history means finding code or text not only in the files as they are now, but in any commit, and finding when something appeared, changed or disappeared. Git has three main tools for it: git grep searches the files of any commit, the pickaxe (git log -S and -G) finds the commits that added or removed some text, and git log -L shows the full history of one function or range of lines. Together with blame and bisect, they answer almost every "when and why did this change?" question.
Think of a newspaper archive. A search box finds every article that mentions a word (grep). An index tells you in which issue a story first appeared and when it was last mentioned (pickaxe). And a follow-up file collects every article about one ongoing story, in order (-L).
The Starting Point
mkdir fee-tracker
cd fee-tracker
git init --quiet
cat > FeeCalculator.java <<'EOF'
class FeeCalculator {
int total(int terms) {
return terms * 12000;
}
}
EOF
echo "*.java diff=java" > .gitattributes
git add .
git commit --quiet -m "Add the fee calculator"
git tag v1.0.0
sed -i.bak "s/12000/12500/" FeeCalculator.java
rm FeeCalculator.java.bak
git commit --quiet -am "Raise the term fee"
cat > FeeCalculator.java <<'EOF'
class FeeCalculator {
static final int TERM_FEE = 12500;
int total(int terms) {
return terms * TERM_FEE;
}
}
EOF
git commit --quiet -am "Use a constant for the term fee"
cat > FeeCalculator.java <<'EOF'
class FeeCalculator {
static final int TERM_FEE = 12500;
int total(int terms) {
return terms * TERM_FEE;
}
int lateFee(int daysLate) {
return daysLate > 0 ? 250 : 0;
}
}
EOF
git commit --quiet -am "Add the late fee"
sed -i.bak "s/? 250 : 0/? 300 : 0/" FeeCalculator.java
rm FeeCalculator.java.bak
git commit --quiet -am "Raise the late fee"
git log --onelinec5d7912 (HEAD -> main) Raise the late fee
c980cf8 Add the late fee
da87a49 Use a constant for the term fee
ef451ff Raise the term fee
c01748e (tag: v1.0.0) Add the fee calculatorgit grep: Search the Files
git grep searches the files Git tracks. It is fast, skips ignored and build files, and can search any commit, without checking it out.
| Command | Description |
|---|---|
git grep <text> | It is used to search the tracked files in the working tree. |
git grep -n <text> | It is used to show line numbers. |
git grep -c <text> | It is used to show the number of matches per file. |
git grep -i <text> | It is used to ignore upper and lower case. |
git grep -w <word> | It is used to match whole words only. |
git grep <text> <commit> | It is used to search the files as they were in a commit. |
git grep -e <a> --and -e <b> | It is used to find lines that match both patterns. |
Example:
git grep -n "TERM_FEE"
git grep -n "12000" v1.0.0FeeCalculator.java:2: static final int TERM_FEE = 12500;
FeeCalculator.java:5: return terms * TERM_FEE;
v1.0.0:FeeCalculator.java:3: return terms * 12000;Explanation: the first search found the constant in the current files. The second searched the files of v1.0.0, where the amount was still written as 12000: the output starts with the commit name, v1.0.0:. To search every commit at once, give it the whole list: git grep "12000" $(git rev-list --all).
The Pickaxe: When Did This Text Appear?
git log -S <text> lists the commits that changed the number of times <text> appears in a file: the commits that added it or removed it. It is called the pickaxe, because it digs out the exact commit.
git log --oneline -S "12000"ef451ff Raise the term fee
c01748e (tag: v1.0.0) Add the fee calculatorExplanation: 12000 appeared in "Add the fee calculator" and disappeared in "Raise the term fee". Commits that merely moved it, or changed other lines, are left out. Add -p to see the diffs, which shows exactly how the text came and went.
git log -G <regex> is the broader version: it lists commits whose diff has an added or removed line matching a regular expression, even if the number of occurrences stayed the same. To see the difference, Navika raises the fee once more, editing the line with the constant:
sed -i.bak "s/TERM_FEE = 12500/TERM_FEE = 13000/" FeeCalculator.java
rm FeeCalculator.java.bak
git commit --quiet -am 'Raise the term fee to $13,000'
git log --oneline -S "TERM_FEE"
echo "---"
git log --oneline -G "TERM_FEE"da87a49 Use a constant for the term fee
---
ed2dc7f (HEAD -> main) Raise the term fee to $13,000
da87a49 Use a constant for the term feeExplanation: -S "TERM_FEE" found only "Use a constant for the term fee", where TERM_FEE went from zero to two occurrences. The new commit changed a line containing TERM_FEE, but the number of occurrences stayed two, so -S ignores it. -G looks at changed lines instead, so it found both. Use -S for "when did this appear or vanish?", and -G for "when was a line containing this edited?".
git log -L: The History of Some Lines
git log -L <range>:<file> shows every commit that changed a range of lines, with only that part of each diff. The range can be line numbers (-L 8,10:FeeCalculator.java), or start at the first line matching a pattern, as here: +2 means two lines, the header of lateFee and the line after it:
git log --oneline -L "/int lateFee/,+2:FeeCalculator.java"c5d7912 Raise the late fee
diff --git a/FeeCalculator.java b/FeeCalculator.java
--- a/FeeCalculator.java
+++ b/FeeCalculator.java
@@ -8,2 +8,2 @@
int lateFee(int daysLate) {
- return daysLate > 0 ? 250 : 0;
+ return daysLate > 0 ? 300 : 0;
c980cf8 Add the late fee
diff --git a/FeeCalculator.java b/FeeCalculator.java
--- a/FeeCalculator.java
+++ b/FeeCalculator.java
@@ -7,0 +8,2 @@
+ int lateFee(int daysLate) {
+ return daysLate > 0 ? 250 : 0;Explanation: only the two commits that touched these lines are listed, newest first, each with the diff limited to them: the method's creation, then the amount change.
-L :<function>:<file> selects a whole function by name instead. For Java it needs *.java diff=java in .gitattributes, which this project has. Note that a whole function includes its closing braces, and lines as common as } can be traced back into older, unrelated commits. A pattern range that covers only the lines you care about gives the clearest history.
Naming Commits for Humans
When a tool gives you a raw commit ID, two commands describe it in words:
git name-rev $(git rev-parse HEAD~1)
git describe --tags HEAD~1c5d7912eff0a44067cdcbfad9fab3a0d26719e9d main~1
v1.0.0-4-gc5d7912git name-rev names a commit relative to a branch or tag ("one commit before the tip of main", written main~1). git describe names it relative to the newest tag, as in "Releases and Semantic Versioning": the number after the tag is how many commits it is past v1.0.0. v1.0.0 is a lightweight tag here, and git describe only uses annotated tags unless you add --tags.
Comparing Branches
Three questions come up whenever two branches exist side by side: what does one have that the other doesn't, where did they split, and which changes were already copied across?
git switch --quiet -c release/1.0 v1.0.0
git cherry-pick --quiet main~4
git switch --quiet main
git log --oneline release/1.0..main
git merge-base main release/1.0
git cherry -v release/1.0 main[release/1.0 27bdb4f] Raise the term fee
Date: Mon Jan 5 10:11:00 2026 -0600
1 file changed, 1 insertion(+), 1 deletion(-)
ed2dc7f (HEAD -> main) Raise the term fee to $13,000
c5d7912 Raise the late fee
c980cf8 Add the late fee
da87a49 Use a constant for the term fee
ef451ff Raise the term fee
c01748ee038654c869be6a34cfd446e96ebddff1
- ef451ffc8ed77d5e58fa1009bb8e86afd673c914 Raise the term fee
+ da87a495c1db1a62eca36dfe6837bd2cf52f83a0 Use a constant for the term fee
+ c980cf87a9d00873d1029c63cc717f7343b8bc1b Add the late fee
+ c5d7912eff0a44067cdcbfad9fab3a0d26719e9d Raise the late fee
+ ed2dc7ff6c7c6c71ad65d9799e696a0c41ff8713 Raise the term fee to $13,000| Command | Answers |
|---|---|
git log --oneline A..B | Which commits are on B but not on A? |
git merge-base A B | Where did A and B split? |
git cherry -v A B | Which commits of B are already on A as copies (-), and which aren't (+)? |
main~4 is "Raise the term fee", the fifth commit from the top. Explanation: release/1.0..main lists the commits on main that aren't on the release branch, including "Raise the term fee". But the release branch has a cherry-picked copy of it. git cherry compares the changes, not the commit IDs, so it marks that one with - (already there), and the others with + (still missing). That is exactly the list a maintainer needs when deciding what else to backport.
Common Mistakes
Searching only the current files for removed code. Grep can't find what was deleted. Use git log -S.
Using -S with a common word. Every commit that added or removed it is listed. Search for something specific, such as a full method name or value.
Forgetting .gitattributes for log -L :method:file. Without *.java diff=java, Git may not recognize indented Java methods.
Expecting git cherry to know about squashed copies. It compares individual commits' changes. A squash combines several, so no match is found.
Plain grep -r in a repository. It also searches .git/ and build output. git grep searches only tracked files and is usually much faster.
Under the Hood
git grep reads files from the index or from a commit's tree directly, in parallel threads, without writing them to disk. The pickaxe works inside git log: for each commit, Git computes the diff against its parent and checks it. -S counts occurrences of the string in the old and new versions of each file and keeps the commit if the counts differ. -G checks whether any added or removed diff line matches the regular expression. git log -L tracks the line range backward through history, adjusting the start and end lines as earlier commits shift them, and prints only the matching hunks. git cherry computes a patch ID for every commit, a hash of the diff with line numbers and whitespace ignored, and compares the patch IDs of the two branches.
Interview Corner
How do you find the commit that introduced or removed a piece of code?
git log -S "<text>" (the pickaxe) lists commits that changed the number of occurrences of the text. Add -p to see the diffs. git log -G "<regex>" finds commits whose changed lines match a pattern.
What is the difference between git grep and git log -S?
git grep searches file content in the working tree or a given commit. git log -S searches the history of changes, finding the commits that added or removed text, including text that no longer exists.
How do you see the history of one method?
git log -L :<method>:<file>, which shows only the commits and hunks that changed that method. For Java, add *.java diff=java to .gitattributes.
How do you find commits on one branch that are not on another?
git log A..B lists commits reachable from B but not from A. git cherry -v A B also tells you which of them already exist on A as cherry-picked copies.
Practice
Challenge: when did the late fee become 300? Use the pickaxe to find the commit that introduced ? 300, and git grep to check whether v1.0.0 had a late fee at all.
What does Git print? Before you open the solution, decide which commit the pickaxe finds, and what git grep prints for v1.0.0.
Show solution
git log --oneline -S "? 300"
git grep -c "lateFee" v1.0.0 || echo "No late fee in v1.0.0"c5d7912 Raise the late fee
No late fee in v1.0.0The pickaxe finds "Raise the late fee", the only commit that added ? 300. git grep found nothing in v1.0.0 and exited with status 1, so the echo ran: the late fee didn't exist in that release.
Checkpoint. In a project of your own, pick a method and read its whole history with git log -L. Then find when a specific constant first appeared with git log -S, and search an old tag with git grep.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git grep -n "<text>" | Search tracked files with line numbers |
git grep "<text>" <commit> | Search an older version |
git log -S "<text>" -p | Find and show commits that added or removed text |
git log -G "<regex>" | Find commits whose changed lines match |
git log -L "/<pattern>/,+<n>:<file>" | History of some lines |
git cherry -v <upstream> <branch> | See which commits are already copied |
git merge-base A B | Find where two branches split |
Was this tutorial helpful?