Git Blame and Archaeology: Tracking Down the Origin of a Bug with git log -S

Git archaeology: find where a line, a function or a bug came from

“Why is this check here?” is a question that the code itself rarely answers. The history does: the commit that added the line, its message, the ticket it references, the lines that changed with it. git blame is the first step, but it only shows the last change of each line, which is often a reformatting. A few options and two git log modes go further back.

Problem: A plugin has a strange condition that skips orders with a zero total in the sync. Someone wants to remove it, but git blame only shows a commit that reformatted the file with PHPCBF two years ago.

Solution: Ignore formatting commits with --ignore-revs-file, follow moved code with git blame -C -C, find the commit that introduced a string with git log -S (the pickaxe), follow a function through all its changes with git log -L :function:file, and read the full commit with git show.

Blame without the noise

git blame -w -C -C -L 120,140 includes/class-sync.php
  • -w ignores whitespace changes.
  • -C -C detects lines that were moved or copied from other files in the same or earlier commits, so a function that was moved from functions.php to a class still shows its original author and commit.
  • -L 120,140 limits the output to the lines in question.

Large formatting commits (a coding standards run, a switch from tabs to spaces) can be excluded for everybody:

# .git-blame-ignore-revs in the repository root
# PHPCBF run on the whole plugin
4a1f2c9e7b3d...
git config blame.ignoreRevsFile .git-blame-ignore-revs

Blame then skips these commits and shows the change before them. GitHub’s blame view reads the same file automatically.

Who introduced this string: the pickaxe

git log -S "get_total() == 0" --oneline -- includes/
git log -G "get_total\(\)\s*[=!]=\s*0" --oneline

-S finds commits where the number of occurrences of the string changed, which means the commit that added it and the one that removed it. -G takes a regular expression and finds commits where a matching line was changed. This works even when the code was moved between files or deleted since; blame cannot find deleted code at all.

The whole story of a function

git log -L :ha_sync_order:includes/class-sync.php
git log -L 120,140:includes/class-sync.php --since=2024-01-01

-L :name:file shows every commit that changed the function, with the diff of just that function each time, from the newest to its creation. For PHP, git finds the function by its name in the function header; methods inside classes work too when the name is unique in the file.

Read the commit in context

git show 7d3e91a                 # message and full diff
git show 7d3e91a --stat          # which files changed together
git log --oneline -5 7d3e91a    # the commit and the ones before it
git branch --contains 7d3e91a    # in which branches it is
git tag --contains 7d3e91a       # first release that shipped it

The commit message and the other files changed in the same commit often explain the reason: here, the zero-total condition came with a fix for free orders that the ERP rejected. A ticket number in the message leads to the discussion. That is the information needed before deleting it.

NOTE: When the question is not “where did this come from” but “which commit broke it”, binary search is faster than reading history: git bisect. Good archaeology depends on good commits; small commits with a reason in the message make all of the above useful: Conventional Commits.