Searching history

Topic 2 of 4 in Power tools7 min readBeginnerFree course

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.

Finding things in history. Who last changed this line? git blame -L 10,20 cart.js. When was this text added or removed? git log -S “applyDiscount”. Which commits changed lines matching a regex? git log -G “price\s**”. How did this function evolve? git log -L :total:cart.js. Where is this text in the code now? git grep -n “TODO”. Which commit message mentions it? git log –grep=“cart” -i. What changed in this file over time? git log –follow -p cart.js.

git blame: who last changed each line?

Terminal
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 pattern
Text
9c8b7a6d (Chen 2026-11-20 14:02:11 +0530 6)   return t * (1 - 0.15); // TODO: configurable

Each 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 -w ignores whitespace changes,
  • git blame -C detects lines moved or copied from other files,
  • git blame <commit>^ -- cart.js blames the version just before that commit, to see what came earlier,
  • a .git-blame-ignore-revs file listing big formatting commits makes blame skip 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:

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

Terminal
git log -G "price\s*\*" --oneline

Combine 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

Terminal
git log -L :applyDiscount:cart.js
git log -L 40,60:cart.js           # or a line range

Git 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

Terminal
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 both

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

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

Terminal
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.js

Putting it together

A typical investigation:

  1. git blame -L '/0.15/,+1' cart.js → commit 9c8b7a6.
  2. git show 9c8b7a6 → “Increase discount to 15 percent” by Chen, referencing issue #88.
  3. On GitHub, issue #88 explains the business decision.
  4. git log -L :applyDiscount:cart.js shows 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 show away.

Exercise

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.

Terminal
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 rename

Summary

  • git blame shows the commit and author that last changed each line; -L limits it to a range.
  • git log -S text finds commits that added or removed text; -G regex finds commits whose changes match a pattern.
  • git log -L :function:file shows the full history of one function.
  • git grep searches the tracked files, in any commit, much faster than plain grep.
  • git log --follow keeps a file’s history across renames.