TIL: Search in git log

Last updated: Wed Apr 10 2024

Yesterday I wanted to look for a block of code in my dotfiles that I deleted a while ago. Turns out there’s an easy way to do that!

git log --oneline -p -S"$TMUX" .config/fish/config.fish

That’ll output a list of all commits that changed the number of occurrences of the string TMUX, alongside the code that commit changed. --oneline cleans up the commit message output, -p generates the patch (basically, the code diff in that commit), and -S does the actual search. It can even filter to a particular file since, in this case, I knew exactly which file I was looking for.

With that command above, I was able to quickly find the last commit where I had deleted all references to the $TMUX environment variable in a script and edit that code for a new use!

Looking at the git-log documentation, there’s also a -G option. The two main differences seem to be:

I’m not sure which is generally better to use — the git-log documentation suggests -S is “intended for the scripter’s use”, whatever that means, and all of OpenAI, Anthropic, and Perplexity suggested -S over -G 🤷‍♀️

References