Git Blame Previous Versions of a File
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:
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:
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).
- c91282ca - The SHA1 hash of the commit involved.
- “Scott Keck-Warren” - The user.name field entered in your ~/.gitconfig
- “2016-12-05 21:02:22 -0500” - The date/time the commit was made
- “15” - The line number of the file
- “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:
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:
And again to see the commit:
Now we can use the ticket number to find the results.
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows