Make your bash aware of git branch on Ubuntu

If you’re someone who has multiple¬† git branches on a project and seem to always do git branch to check which branch you’re in. Then here’s how you can make your bash aware of a git branch.

Well, I’m not sure if this works on others, but it sure does works in my Ubuntu Hardy. Just edit your .bashrc at your home folder.

vim .bashrc

Add the following at the very bottom of your .bashrc

[sourcecode language=’cpp’]

parse_git_branch() {
¬† git branch 2> /dev/null | sed -e ‘/^[^*]/d’ -e ‘s/* \(.*\)/(\1)/’
PS1=”${debian_chroot:+($debian_chroot)}\u@\h:\w\$(parse_git_branch) $ ”

After that, save it and restart your terminal. You should see something like this at your terminal:

fadhli@atlantis:~/projects/crimson_mdec(master) $

I have a branch named biz_idea, so after a git checkout biz_idea

Switched to branch “biz_idea”
fadhli@atlantis:~/projects/crimson_mdec(biz_idea) $

Tags: ,

One Response to “Make your bash aware of git branch on Ubuntu”

  1. TimVS December 5, 2012 at 5:12 pm #

    Thanks for this tip.
    This is really helpful

Leave a Reply