One of the downsides to working on a project for a long time is that it’s hard to remember why changes get made. If you have a good comment policy the comments can be helpful to determining why something is the way it’s done (I recommend at the very least you include a ticket number and a helpful description of the change) but the “problem” with git blame is that “hides” previous version. While a line might have been changed yesterday it’s reason for existing might be hidden from you directly. This article will explain how to view the previous commits for a line so you can determine why it was added/change/whatever.

Let’s say we have the following code that start on line 15 of a file:

if ($this->getTest() && $this->getOtherTest()) {
    $this->goSomething();
}

We’re interested in why $this->getTest() is part of the if clause. So we can quickly see what changes have been made and not have this post take up 15 pages, in our example file the if is on line 15 (that’s why we have -L15 in the commands below).

Let’s run git blame on the file and see our results:

$ git blame -L15 "test.php"
c91282ca (Scott Keck-Warren 2016-12-05 21:02:22 -0500 15) if ($this->getTest() && $this->getOtherTest()) {
fcc313cc (Scott Keck-Warren 2016-12-05 21:02:04 -0500 16)     $this->goSomething();
fcc313cc (Scott Keck-Warren 2016-12-05 21:02:04 -0500 17) }
fcc313cc (Scott Keck-Warren 2016-12-05 21:02:04 -0500 18) 

In case you’re not sure what you’re looking at let’s break it down a little (using the first line as an example).

  1. c91282ca - The SHA1 hash of the commit involved.
  2. “Scott Keck-Warren” - The user.name field entered in your ~/.gitconfig
  3. “2016-12-05 21:02:22 -0500” - The date/time the commit was made
  4. “15” - The line number of the file
  5. “if ($this->getTest() && $this->getOtherTest()) {“ - the line as it existed in that commit.

So the important part about this blob of stuff is the “c91282ca” because that will allow up to use git show to lookup that commit:

$ git show c91282ca
commit c91282caca5e8bd1b3d8072290345c6c8bbe9530
Author: Scott Keck-Warren <warren5236@gmail.com>
Date:   Mon Dec 5 21:02:22 2016 -0500

    Updated if block [ticket 100]

diff --git a/test.php b/test.php
index 0efcb09..b551c4c 100644
--- a/test.php       
+++ b/test.php       
@@ -12,7 +12,7 @@ author: warren5236
 ---
-if ($this->getTest()) {
+if ($this->getTest() && $this->getOtherTest()) {
     $this->goSomething();
 }

As you can see in this commit, we added to the line and the actual creation reason has been hidden using a basic blame. But now that we have the commit the line was changed in we can use that as the baseline to look for the change before that:

$ git blame -L15 c91282ca^ -- "test.php"
fcc313cc (Scott Keck-Warren 2016-12-05 21:02:04 -0500 15) if ($this->getTest()) {
fcc313cc (Scott Keck-Warren 2016-12-05 21:02:04 -0500 16)     $this->goSomething();
fcc313cc (Scott Keck-Warren 2016-12-05 21:02:04 -0500 17) }
fcc313cc (Scott Keck-Warren 2016-12-05 21:02:04 -0500 18) 

And again to see the commit:

$ git show fcc313cc
commit fcc313cc38e37400cd42a933ce86af1b0d7cc0cf
Author: Scott Keck-Warren <warren5236@gmail.com>
Date:   Mon Dec 5 21:02:04 2016 -0500

    Added if block [ticket 1]

diff --git a/test.php b/test.php
index 05851b0..0efcb09 100644
--- a/test.php       
+++ b/test.php       
@@ -12,5 +12,9 @@ author: warren5236
 ---
 
+if ($this->getTest()) {
+    $this->goSomething();
+}
+

Now we can use the ticket number to find the results.