Luke Oliff.

TIL: The git log Pickaxe Finds the Commit That Deleted a Line

·TIL·2 min read·Luke Oliff
TL;DR

The git log pickaxe is git log -S "some text". It lists only the commits that changed how many times that text appears, so you get the commit that added it and the one that deleted it. git log -G also catches commits that edited a line with the text in it.

git log -S finds the commit that added or deleted a piece of text. Git calls it the pickaxe, and the git log pickaxe is the quickest way I know to answer “which commit took this function out?”

git log -S "retryRequest" --oneline

Git keeps only the commits that changed how many times retryRequest shows up, so you get the one that added it and the one that deleted it. Add -p to see each change.

git bisect tests commits one at a time to find what broke. If you already know the text, you do not need to test anything.

When the git log pickaxe skips a commit

Changing retries = 3 to retries = 5 doesn’t change how many times retries appears, so -S retries leaves that commit out. Use -G for those:

git log -G "retries" --oneline

-G takes a regex (a search pattern) and keeps any commit where an added or removed line matches it, so edits show up.

Can I search one file?

Yes. Put the path after two dashes, like git log -S "retryRequest" -- src/http.ts. Git then only looks at changes to that one file. On a repo with years of history, that makes the git log pickaxe a lot faster.