Who changed this, and why
Last updated
Bisect found which commit broke the adobo. Often you start from the other end: you're looking at a line and want to know where it came from. In this lesson you'll ask Git who last changed each line, when a piece of text first appeared, and where a word occurs in any version, even across a rename.
By the end of this lesson you can
- Use git blame to find the commit behind a line, and see past formatting commits
- Use git log -S and -G to find when text appeared or vanished
- Search any version with git grep
- Follow a file's history across a rename
Blame: who last touched each line
git blame <file> prints every line with the commit that last changed it, its author and its date:
git blame -L 11,13 adobo.md d9c81bc9 (Joaquin Cruz 2026-03-12 11:20:00 +0800 11) - 4 bay leaves 601c654b (Ana Reyes 2026-03-02 09:07:00 +0800 12) - 1 tsp whole peppercorns a5e3d675 (Ana Reyes 2026-03-12 10:52:00 +0800 13) - 1 tbsp salt
-L 11,13 limits it to lines 11 to 13. Then git show a5e3d675 shows the whole commit: its message, and what else it changed. On a hosting site, the commit page also links to the pull request it came from, where the discussion is.
The name is unfortunate. Blame answers "which change, and why?", and the answer is usually a reason, not a culprit.
Text appearing and vanishing: -S and -G
Blame shows only the last change to a line. To see the whole story of some text, search history for it:
git log -S "1 tbsp"lists commits that changed how many times1 tbspappears: where it was added or removed. (You met it in lesson 7.6.)git log -G "salt"lists commits whose diff has an added or removed line matching a pattern, even if the count didn't change.- Add
-pto see each commit's diff.
And git grep searches files: the working folder by default, or any commit: git grep "bay" HEAD~10.
Investigate the salt
The Kusina from lesson 8.4, with the salty adobo still on main.
- Find who last changed the salt line with
git blame adobo.md - Show that commit with
git showand its hash - List every commit that added or removed “1 tsp salt” with
git log -S - List every commit whose changes mention salt with
git log -G salt --oneline - Search an old version: how many bay leaves did HEAD~10 have? (
git grep bay HEAD~10)
You are in ~/kusina. Somewhere in the last 24 commits, the adobo became far too salty.
git log -S "1 tsp salt" finds two commits: the one that added the line and the one that removed it. Together they're the line's whole life.
Following a renamed file
Renaming a file in Git is a delete and an add (lesson 8.1: the blob doesn't change, only the tree's names do). Git notices renames by comparing contents. But git log <file> only looks for the file's current name, so its history seems to start at the rename. --follow keeps going back under the old name:
- Rename the recipe with
git mv adobo.md chicken-adobo.mdand commit - Run
git log --oneline chicken-adobo.md: one commit - Run it again with
--follow
You are in ~/kusina. Somewhere in the last 24 commits, the adobo became far too salty.
--follow works for one file at a time. git blame follows renames by itself.
Spot the trap: blaming the formatter
Carla runs blame on build_index.py to ask who wrote a strange line. Every line says Joaquin, “Format with ruff”. She messages Joaquin: “Why did you write this?”
What happens if…
# Format with ruff 5b1c0e2d4f6a8b0c2d4e6f8a0b2c4d6e8f0a2b4c
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Git documentation: git-blame: -L, --ignore-rev and --ignore-revs-file
- Git documentation: git-log, -S, -G and --follow: the pickaxe options, and following renames
- Git documentation: git-grep: searching the working tree or any commit
- GitHub Docs: Viewing a file, ignore commits in the blame view: the host reading .git-blame-ignore-revs