DEV Community

@lukeocodes 🕹👨‍💻
@lukeocodes 🕹👨‍💻

Posted on Originally published at lukeocodes.dev on

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

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)