Master Git (part I). Commit search.

Git log command lets you navigate through the history of your commits. I personally use the following git log command and alias for it quite often:

# ~/.gitconfig
  l = log --all --decorate --oneline --graph

which prints the history of the commits, where it has diverged, commit messages and local branches.


The coolest thing about git log is that it provides you with the handy options for search filtering. For example, it allows you to base your search on a specific time frame or author. I find particularly useful search by the message and content.

Grep commit messages

Git allows you to grep commit messages. For example, you could use this simple command below to find all the commits related to a specific task in JIRA:

$ git log --grep="JIRA-30"

But I prefer to customize this command a bit, changing the output to my liking. So I created the following alias in my ~/.giconfig file:

    findm = log --pretty=\"format:%Cred%h %C(bold blue)%s %n %C(yellow)<%an> %Cgreen(%cD)\" --all --name-status --grep

Now I can use git findm "JIRA-30" to see all the commits across all branches (--all) which have JIRA-30 in their message. In the output I will get abbreviated commit hash (%h) in green, a commit message (%s) in blue, and information about the author and data of the commit in red (%an, %cD).


See commits that affected a file

In case, you would like to see commits that changed a particular file(s). You can use the following command:

git log -- <path/to/file>

For such cases, I use the following alias:

    findf = log --pretty=\"format:%Cred%h %C(bold blue)%s %n %C(yellow)<%an> %Cgreen(%cD)\"


Search by the commit content

If you want to find a commit when a specific line of code was introduced or deleted, you can make use of -S option of git log command:

$ git log -S "Hello, World!" # use -G for regular expressions

Real world example: recently I faced the problem that one of the community cookbooks I was using for quite some time started to fail. The output from chef run gave me the following error:

  No resource or method named `cached_file' for `LWRP provider seven_zip_archive from cookbook seven_zip ""'

For people who are not familiar with Chef, to put it in most basic terms, a cookbook could be compared to a script that configures the system. Often those scripts use classes and methods defined in another cookbooks (scripts). And here we see the problem, when changes in one script affect the work of another.

You can see from the output above that it was a seven_zip cookbook and inside its providers some unknown cached_file method was called.I looked into the cookbook’s code and found out that this cached_file was part of the windows_helper which in turn is part of the windows cookbook. I looked at the current version of this helper and didn’t find any definition of cached_file method, but apparently there was supposed to be one.

So I cloned the repository on my machine. And ran

$ git log -S 'def cached_file' --oneline

to see if there was any mentioning of this method inside windows cookbook repository. And I found there was indeed three of them:


Considering the fact that git log -S shows commits when the code was introduced or deleted and that latest version of files didn’t have it, obviously the first commit in this list should be the one when cached_file method was deleted.

I was able to confirm it with git show <commit> command which showed me the changes brought by this commit:


If you prefer looking at the commit on github you can use this URL template:<owner>/<project>/commit/<hash>

So in my case, I went to

With the help of this git log command, I was able to quickly find when the code was removed and which version of the windows cookbook was still good to use.

P.S. Soon after they added the method back to the windows_helper. Apparently, there were more cookbooks that used this helper’s method.