Get "PHP 8 in a Nuthshell" (Now with PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Fetch commit logs of certain function or method in Git

You would reach for the git log command when you want to take a look at the list of commits for the Git repository you’re currently in.

The command will list all the commits for that repository showing the newest commit first.

A typical output for git log would look like so.

So, that’s a repository-wide git log but there comes a time when you want to have logs for a specific piece of code for a specific file.

For instance, you might need to check in which commits exactly a certain method/function has been changed. How would you do that?

Well, the git log command can let do exactly that. It’s called “Line Log Search”. Let me show you how.

Let’s say I want to check whether in which commits the showToast method in the js/utils.js file has been changed. I can do that by using the git log command with the -L option like so.

$ git log -L :showToast:js/utils.js

This is how the output would look like.

As you can tell, the command will intelligently try to figure out what the boundaries of the function are and then look through the history and show us every change that was made to the function as a series of patches back to when the function was first created.

A really handy tool in certain situations!

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?