David's Blog

Searching Git commit diffs

Today I read One Year of TILs and thought that recording the stuff I learn in public seems like a great idea for many of the reasons Simon outlines.

I don't have a particular TIL from today but thought I'd start by sharing some git functionality that I recently learned and have been using an increasing amount in git history spelunking. There are plenty of tools for searching directories (whether they're repositories or not): grep, ripgrep, git grep, livegrep, and many others. Sometimes this is not enough - when searching through git history, sometimes there's a string you want to find at some point in the git history but you don't know when it changed.

git log -S and git log -G to the rescue!

The -S option allows providing a string that will be used to search for commits that added/removed uses of that string with an exact match. This is useful for finding historical modifications or uses of a structure, function, etc. The -G option does the same but with a regular expression instead of a string. As someone who digs into history, logs, and blame frequently to find out why something changed or when something stopped/started being used, finding these options has been super useful!

Check out the git manpages locally or online for more details.