Which Branch am I in?

This is the introductory post to Coding in the Cortex area of our blog.  The plan for this section of the blog is to discuss coding related topics that may be of interest to other indie developers.

At Infinite Cortex Creations we are using Git for managing our source code and other critical documents.  One of the nice things about Git is the ability to create and merge branches.  This allows us to try out new features or experiment with different ideas without having to commit these changes to the master branch until they have been verified and tested.

But, when working with branches it can be easy to get confused and forget which is the current branch.  Xcode presents this information when using the Repositories tab of the Organizer and gives you an easy way to switch between branches.

But, I don’t alway work with Git from within Xcode. I often find myself working with Git on the command line in a terminal window.

Of course you can use the command git branch to see a listing of your branches with the current branch flagged with an *.

$ git branch
 f_fly_class_improvements
 * f_game_engine
 f_new_scenes
 master
$

But, I would rather be able to see my current branch with out the need to enter a Git command.  You can accomplish this with a bit of bash shell scripting. If you don’t use bash you should still be able to adapt this technique for your particular terminal shell.

To accomplish this you will need to edit your bash profile and set a custom prompt.  Use the text editor of your choice to edit the file named .bash_profile in your home directory.  The leading . indicates that this is a hidden file so you may have to set your text editor to show hidden files. In this file add the first add the following function.

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

When this function is called it will invoke the git branch command and then parse the list to return the current branch name surronded by square brackets.  So for our branch list shown above it would return [f_game_engine].

Now we need to update the prompt to use the output of our function.  To do this we will override the default PS1 value by setting a new value in our .bash_profile.

PS1="\h:\W\[\033[0;32m\]\$(parse_git_branch_and_add_brackets)\[\033[0m\]\$ "

This will cause the terminal prompt to be the current host name (\h), followed by a colon, then the current directory name (\W), the branch name colored green followed by the $.

So if you save the .bash_profile file, open a new terminal window and go to the directory of one of your Git projects you will see something like this:

dev1:fff_code[f_game_engine]$

Where “dev1” is the host name of my computer. “fff_code” is my current directory. and “f_game_engine” is my current brach.

I find that adding this has helped prevent accidental commits into the wrong code branch.  Hope that others my find it useful as well.

Posted in Coding and tagged .