by Jerad Bitner

Git Best Practices: History Viewing Tips and Displaying Branch Context

The Git version control tool provides a number of ways to examine the history of a particular file or directory. When rolling a new version of a Drupal module, or documenting changes that have been made to a site, capturing that information can save quite a bit of time. In a perfect world, you would already have a /patches directory in your repository root with the individual changes and a README.txt describing each one, or even a complete Drush .make file that captures all of the module versions and patch files used to build your project. But the world isn't always perfect... And when the time comes to track down what changes have been made to the code in your repository, using Git's history log is the solution. The quickest way to review history is through command line.

$ git log [<path>]

This prints out a quick list in the following format:

commit 86ea8974821d6adaa198901b0fd5a3c046ade59c
Author: Jerad Bitner <obscured>
Date:   Wed Mar 23 19:37:20 2011 -0600

    adding the '/user' path with the same form id and using the paths as the selectors in the jquery

commit 7e965dd170d812324bc65833f67cc33d3dddc375
Author: Jerad Bitner <obscured>
Date:   Wed Mar 23 16:52:09 2011 -0600

    reload the window after a form submission

Nice for a quick view, but maybe you want something a little prettier.

$ git log --oneline [<path>]

This prints out a quick list in an even shorter format:

86ea897 adding the '/user' path with the same form id and using the paths as the selectors in the jquery
7e965dd reload the window after a form submission
927f2b6 adding an administrative page to customize width/height per form - also an alter hook to add other forms to the list (maybe I should call this modalframe_forms?)
c5979f2 initial commit of modalframe_login

I also like to use Gitx for my history viewing needs. It's a nice little GUI tool for the Mac that makes history viewing a more pleasurable experience IMHO. With it, you can quickly see branch history, merges, and any timeline changes. It also has command-line integration so that you can execute it from within your Git repository's checkout.

$ cd ~/
$ gitx

If you click on the 'Tree View' button at the bottom, you can get a list of all of the files and directories within your repository, and by right clicking on any file or directory, can then view the full history of just that file or directory.

So if you need to check what's going on with just one file or directory, Gitx is great. There are many other awesome GUI's out there now for Git, but I tend to stick to the basics.

What branch am I on?

Another great tool for visualizing where I am is a simple script in my ~/.bash_profile that parses the current directory for a .git directory and if found, will print out the name of the branch in my shell's command prompt.

## Bash prompt
function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'

function proml {
  local        BLUE="\[\033[0;34m\]"
  local         RED="\[\033[0;31m\]"
  local   LIGHT_RED="\[\033[1;31m\]"
  local       GREEN="\[\033[0;32m\]"
  local LIGHT_GREEN="\[\033[1;32m\]"
  local       WHITE="\[\033[1;37m\]"
  local  LIGHT_GRAY="\[\033[0;37m\]"
  case $TERM in

$BLUE[$RED\$(date +%H:%M)$BLUE]\
$GREEN\$ "
PS2='> '
PS4='+ '

The result of which is to turn this:


into this:


and if I switch into the 6.x-1.x branch from the master branch, then you can see that the command line indicator shows me this on each new line:

[08:21][sirkitree@sirkitbox:~/]$ git checkout 6.x-1.x

Branch indicators have really become an integral part of my workflow, one I don't think I could live without.

I hope you find these tips useful, and I'll be bringing you more as a part of a series as I work with Git on Do you have any tips to share with us?