March 5, 2021
Hot Topics:

10 Git Tips and Tricks for Beginners

  • By Jason Gilmore
  • Send Email »
  • More Articles »

6. Reviewing a Previously Committed File's Contents

I often need to examine the contents of an earlier file revision after making some changes to the current version, without actually restoring the file. You can easily do so using the show command accompanied by an argument that points to the file:

%>git show HEAD^:application/controllers/AboutController.php 

The caret (^)is representative of the number of revisions backwards you'd like to look. Therefore the above example will display the AboutController.php file's previous revision. If you'd like to look back three revisions you would use three carets, like this:

%>git show HEAD^^^:application/controllers/AboutController.php 

Alternatively, you can use the commit's hash to reference the file. For instance if you'd like to view the contents of the AboutConroller.php file as it was committed five revisions ago, you could execute git log to determine the commit hash, and then use the first five characters of that hash to retrieve the file contents:

%>git show 23aa985:application/controllers/AboutController.php 

7. Editing Your Most Recently Committed Log Messages

I'm a stickler for spelling, but in my haste to commit the latest changes I occasionally will slip an unwanted letter or two into the message. You can easily edit the most recently committed message using the amend command:

%>git commit --amend 

Executing this command will load the latest commit message into a text editor, allowing to you edit and save the message.

8. Stashing Uncommitted Changes

Occasional interruption of your programming routine is inevitable, often putting you in a position of not being quite ready to commit changes, yet suddenly needing to fix and commit an issue unrelated to your current work. You can tuck away your current changes using the stash command, thereby reverting your base to the previous commit point, allowing you to make and commit the new changes. When complete, you can return to your stashed state. For instance, suppose I was working on a new section to a project's README document, and suddenly noticed an egregious spelling mistake. I can tuck away my current changes like this:

%>git stash save

Opening README anew, I would see that the new section has disappeared, because I returned to the previous commit point. I would then fix the spelling error and commit the changes. Then execute the following command to return to my original state:

%>git stash pop

9. Browsing Your Repository

Quite a few Web-based interfaces are available for browsing Git repositories, but did you know that one named instaweb is integrated into the native distribution? To peruse your repository within the browser, execute the following command:

%>git instaweb --httpd apache2 

Passing apache2 to the --httpd switch will tell Git that Apache is the web server running on this machine. The default is lighthttpd, although several other servers are also supported.

10. Blaming Somebody Else

Occasionally a team member (but never you, of course) will introduce a bit of untested code into the repository and break your build. Naturally you'll want to blame somebody for the problem, but who introduced the error? Use the blame command to find out:

%>git blame application/controllers/AboutController.php 

23aa9852 (Jason Gilmore 2010-06-03 12:34:04 -0400 11) public function indexAction() 
23aa9852 (Jason Gilmore 2010-06-03 12:34:04 -0400 12) { 0e9e9f49 (Jason Gilmore 2010-06-03 13:32:47 -0400 13)
echo "Missing semicolon" 23aa9852 (Jason Gilmore 2010-06-03 12:34:04 -0400 14) }



Git is truly a treasure trove of fantastic features that make source code management a breeze. Which Git features, tips and tricks do you find useful? Tell us about them in the comments!

About the Author

Jason Gilmore is the founder of EasyPHPWebsites.com. He also is the author of several popular books, including "Easy PHP Websites with the Zend Framework", "Easy PayPal with PHP", and "Beginning PHP and MySQL, Third Edition".

Page 2 of 2

This article was originally published on June 5, 2010

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date