Pages

Thursday, August 30, 2012

Add Git Branch to Bash prompt and a git goodie - SourceTree

Adding git branch to your bash prompt has become easier than before.

Edit your ~/.bash_profile to add following code

# Set git autocompletion 
if [ -f /usr/local/git/contrib/completion/git-completion.bash ]; then
  . /usr/local/git/contrib/completion/git-completion.bash
fi

GIT_PS1_SHOWDIRTYSTATE=true

if [ -f /opt/local/etc/bash_completion ]; then
    . /opt/local/etc/bash_completion
fi

PS1='\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '

It not only adds branch name but also indicates the current status of your project. For example, when you add files to staging area, + is added after branch name. 
If you make further changes to staged files, it adds * after branch name to indicate there are unstaged files.

The above script also enables Git autocompletion which will help saving some keystrokes.

If you prefer GUI tool and even if you prefer command line, SourceTree is a must have tool for Mac users. It shows you all branches, their commits, the changes. You can do everything using it.


For newbies, it will make using git a lot more fun!

2 comments:

  1. Thanks for the tip. Definitely useful for power Git users.

    ReplyDelete
    Replies
    1. Thanks for reading :) I never expected Google geek would read my blog :)

      Delete