Git Searching

Git Searching


Have you ever wanted to search for something using git?

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.

Git Bash Console

File  Edit  View  Help

User@DISCOVERY ~/Projects/Sample (develop)
$ git log
commit 4f0cafaad7d1f2d702d77b326a8c38c181a8efe2
Author: Donald Fagen<donald.fagen@email.com>
Date: Tue Jan 28 12:17:51 2014 -0600

DEFECT2806 – (WIP) Added pre-import check for duplicate names
Changed the name of the Admin Admin (STAFF_ID = 1) staff member to Import STAFF_ID = 1

commit 21527ef0e5a7c6e130d93208f8af12ff0b6cbfeb
Merge: 883c998 06f8056
Author: Michael McDonald <michael.mcdonald@email.com>
Date: Tue Jan 28 08:51:12 2014 -0800

Merge pull request #196 from offshore/d6796_closebutton

Defect 2796 – Added close button in popup as per UI design specifications

commit 883c998b8f0407c742967e0fde836a8d4d1191c0
Author: Barry Manilow <barry.manilow@email.com>
Date: Tue Jan 28 09:01:16 2014 -0600

Fixing a sql syntax issue in the database migrator

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.

Git Bash Console

File  Edit  View  Help

User@DISCOVERY ~/Projects/Sample (develop)
$ git log –oneline

4f0cafa DEFECT2806 – (WIP) Added pre-import check for duplicate names Changed the name of the
I21527ef Merge pull request #196 from offshore/d2796_closebutton
883c998 Fixing a sql syntax issue in database migrator
06f8056 Defect 2796 – Added close button in popup as per UI design update
bddb1d6 Defect 2796 – Performance improvement for grid
9e5e2b3 Removed Order By Clause that was causing headaches
83fa92a Merge branch ‘Release12.2.1’ into develop
c83dc9e D2775 Merged in Release12.2.1 More than one match filters
160fef3 Merge pull request #192 from offshore/d6796
aa2d6fc Merge branch ‘DE2808’ into Release12.2.1

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.

Git Bash Console

File  Edit  View  Help

User@DISCOVERY ~/Projects/Sample (develop)
$ git log –oneline –max-count=5

4f0cafa DEFECT2806 – (WIP) Added pre-import check for duplicate names Changed the name of the
I21527ef Merge pull request #196 from offshore/d2796_closebutton
883c998 Fixing a sql syntax issue in database migrator
06f8056 Defect 2796 – Added close button in popup as per UI design update
bddb1d6 Defect 2796 – Performance improvement for grid

User@DISCOVERY ~/Projects/Sample (develop)

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’

Git Bash Console

File  Edit  View  Help

User@DISCOVERY ~/Projects/Sample (develop)
$ git log –pretty=format:’%h %an > %s’
4f0cafa Donald Fagen > DEFECT2806 – (WIP) Added pre-import check for duplicate staff names Changed the name of the Admin Admin
21527ef Michael McDonald > Merge pull request #196 from offshore/d2796_closebutton
883c998 Michael McDonald > Fixing a sql syntax issue in database migrator
06f8056 Barry Manilow > Defect 2796 – Added close button in popup as per UI design update
bddb1d6 Barry Manilow > Defect 2796 – Performance improvement for grid
9e5e2b3 Donald Fagen > Removed Order By Clause that was causing headaches
83fa92a Bruce Springsteen > Merge branch ‘Release12.6.0’ of develop into Release12.6.2
c83dc9e Bruce Springsteen > D2775 Merged in Release12.6.0 More than one match filters
160fef3 Michael McDonald > Merge pull request #192 from offshore/d2796
aa2d6fc Michael McDonald > Merge branch ‘DE2808’ into Release12.6.0

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

Git Bash Console

File  Edit  View  Help

User@DISCOVERY ~/Projects/Sample (develop)
$ git log –pretty=format:’%h %an %s’ | grep McDonald
21527ef Michael McDonald Merge pull request #196 from offshore/d2796_closebutton
883c998 Michael McDonald Fixing a sql syntax issue in database migrator
160fef3 Michael McDonald Merge pull request #192 from offshore/d2796
aa2d6fc Michael McDonald Merge branch ‘DE2808’ into Release12.6.0

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

Git Bash Console

File  Edit  View  Help

User@DISCOVERY ~/Projects/Sample (develop)
$ git log –pretty=format:’%h %an %s’ | grep McDonald | grep sql
883c998 Michael McDonald Fixing a sql syntax issue in database migrator

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.

Git Bash Console

File  Edit  View  Help

User@DISCOVERY ~/Projects/Sample (develop)
$ git diff 883c998 883c998^
diff –git a/src/Tools/DataMigrator/TOPAZ.DataMigrator.Core/Migrators/MultiSite/UserProfileMigrator.cs b/src/Tools/DataMindex f558446..4e22b3d 100644
— a/src/Tools/DataMigrator/TOPAZ.DataMigrator.Core/Migrators/MultiSite/UserProfileMigrator.cs
+++ b/src/Tools/DataMigrator/TOPAZ.DataMigrator.Core/Migrators/MultiSite/UserProfileMigrator.cs
@@ -26,7 +26,7 @@ namespace Company.Sample.Migrators.Staff
var updateStatements = new List();

– When.Counting(string.Format(“SELECT COUNT(1) FROM ({0}) TBL”, GetStaffSql()));
+ When.Counting(string.Format(“SELECT COUNT(1) FROM ({0})”, GetStaffSql()));

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.
Cheers!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s