Skip to content

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

Erica Pisani
Erica Pisani
1 min read
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.

gittips-and-tricks

Comments


Related Posts

Members Public

Using XOR to write concise conditionals

It's not uncommon that I sometimes write if statements where the overall conditional is true when both conditions are true or both are false. As an example, let's say I'm validating input from an API call where I'm updating information on a

Members Public

Fixing "No preset version installed for command poetry"

Poetry is a packaging and dependency management tool for Python, and a tool that didn't exist when I worked with Python many years before I started working at Float. I also hadn't been exposed to asdf, which is a handy little tool for managing multiple runtimes.

Members Public

How to install a Nerd Font on Warp

I started using Vim as my IDE of choice recently, and one of the things I wanted early on was a more visually appealing icon and font set. I kept reading about Nerd Fonts so I decided to try installing one, but the instructions for doing so were both clear