Git Log's Hidden Gems: Using -S and -L for Powerful Code History Search

ericapisani

Erica Pisani

Posted on November 10, 2024

Git Log's Hidden Gems: Using -S and -L for Powerful Code History Search

Ever needed to track down when a specific piece of code was first introduced in a project?

As part of some refactoring I had to do recently, I needed to do just that for a variable on a Django model.

I was already familiar with the basic git log command, but I didn't realize until I needed to find this information that git log has additional options available that give me some powerful search capabilities in the history of a codebase.

The first one, and the one that helped me find the answer to my question, was -S, also known as the Git 'pickaxe' option.

git log -S gets Git to show you only commits that changed the number of occurrences of the string that's being searched for. As an example:

git log -S property_i_am_interested_in

In learning about this command in Git's documentation, I also noticed a -L option. This enables 'line log' search, which allows you to view the history of a function or line of code in a given file. For instance:

git log -L :some_method_name:file_whose_history_you_are_interested_in

And, if you need something more specific in your search in either of these cases, both of these options also support regexes.

While source control providers like Github expose some of this functionality in their web interfaces, sometimes you don't want to go through the hassle of leaving the CLI, in which case these git log options can be very helpful.

If you're interested in taking a closer look at the docs, along with some examples, you can check out the official Git documentation here.

πŸ’– πŸ’ͺ πŸ™… 🚩
ericapisani
Erica Pisani

Posted on November 10, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related