Adding git Branch and Commit Info to your Prompt
A few months ago we started using git for source control. It has a bit of a steep learning curve, but branching, tagging, and vendor management are all much easier once you wrap your head around the git approach.
I did get caught by one snag recently though. When upgrading from rails 2.0.2 to 2.1, I inadvertently checked out the branch, rather than the tag, and ended up with all sorts of nasty errors. Since nothing gave me an obvious indication of which tag/commit I was working with, it took me a good while to figure out what had happened.
Vowing to never waste time on that mistake again, and knowing it was probably a good idea to know where I was at all times anyway, I went in search of a solution. I soon came across this little gem of a script from Jon Maddox at simplisticcomplexity.com.
With Jon’s script you get a bash prompt that includes the branch and looks like this:
![]()
Pretty sweet! Unfortunately, it doesn’t really solve my original problem, and the colors aren’t my style either. My whole problem arose because I checked out the correct branch, “2-1-stable”, but was on a commit after the one tagged “v2.1.0″. Probably one of those one in a million things, but I don’t want it to happen again.
So with a little work I modified Jon’s script to provide the commit and a bit more style, like so:
![]()
In this example from the directory that started it all, the branch is listed as “detached” to remind you that you are not working with the HEAD revision, and the commit tag description is provided from the nearest actual tag.
You will more commonly see something like this in your everyday use:
![]()
Knowing exactly where in the code I am at all times has since proved very useful and alerted me to other potential issues, like when I forget to merge into the staging or production branches before deploying.
Just grab the script (pastie) and put it at the top of your .bash_profile
Tags: git, programming, rails
1 comment
September 24th, 2009 at 4:52 am
You can do it by modifying your ~/.bashrc file. After quick search I found following script you can add to your ~/.bashrc file.