Have you ever wanted to search your git repository for something? Sometimes it’s useful to be able to search through the git log to find out when a change from a story or defect occurred. I did and found out that with a little knowledge, you can leverage git’s features to help you quickly find what you’re looking for. The other day at work I was asked to implement a fix that was added to an newer version of our software so that it could be applied to an older version for a software patch. Seemed like a simple request at the time. I knew the defect number, the functionality that was fixed, and the approximate time that the defect was fixed. Not a problem right?
Wrong. First I had to find out what was unique about the entry in the git commit log to identify the commit. Only once I identified that, could I see which files where changed to implement the fix. This would have been easy if the person who committed the change had included a change comment that included the defect number in the commit message, but they didn’t. This can be prevented using git’s hooks. You can read more about this in this post on client side git hooks.
Let’s begin with the simplest of commands to view the history of a particular branch. Open up your git bash console and navigate to the directory that you want to search. Once your in a folder that contains a git repository in it; type the command git log.
You can hit the space bar to go a page at a time or the enter key to move a line at a time. When you are done looking at the log, press ‘Q’ to quit. Depending on how much the users put into the commit comments this could get quite verbose, and time consuming. There has to be a better way. What if we condensed the information that we look at? Let’s try this by asking git to format the results for us. Issue the command git log –oneline at the bash prompt.
This reduces the amount of noise that I have to look through to read the comments. Depending on the height and width of the bash console window, you can skim through the information quickly in order to find what you want. While you can see more information here, this still doesn’t help you narrow down what your looking for. If the project is long running this list could be extensive. How can we reduce the amount that we are looking at? Let’s ask git to reduce it for us with this command: git log –oneline –max-count=5. This will ensure that only the last 5 results will be output to the screen. This can be useful if the commit was rather recent.
Even with a reduced amount of data to look at, unless the commit message has the appropriate information in it to catch your eye, you may never find it.. How can we see who authored the commit? Let’s use the following command: git log –pretty=format:’%h %an > %s’
Now we can see a bit more, we can see the shortened git commit SHA hash, the author, and the commit comment. Now I can filter on the name of the user if I know who actually committed the fix to the repository. I’m going to filter down my results by piping the output of the git log command to grep. Grep is an old Unix command used to search files for the occurrence of a string of characters that matches a specified pattern. It is still active as part of the shell that bash uses. Let’s try git log –pretty=format:’%h %an %s’ | grep McDonald
As you can see this really reduced the amount of data that we needed to look for. Instead of searching for the change author, we could have searched the commit message for the specific defect number D2775 if that was added to the commit message. What if I want to filter down inside one of the commits for the author? You can always pipe the output of your grep into another grep command to further filter your results for you. In the following example, I am going to filter all of Michael McDonald’s commits and only examine the one that is a fix for a SQL syntax. The command git log –pretty=format:’%h %an %s’ | grep McDonald | grep sql
Now that you have narrowed it down to a single commit, you can use the shortcut SHA in git diff to see what was actually changed. Here is an example of what that would look like.
I hope that this helps you find what your looking for. I know it helped me. If you have any questions drop me a line.