Git and Bash Love

30 Jun 2011 - git

I’ve recently been working with git a lot, more specifically with branches. I love git’s branching and its fairly awesome. However, its also easy to forget which branch you’re currently working on (especially awful to find that the commits you did were to master :/). Sometime back I did some extensive searching to fix that situation and finally stumbled upon a neat way to fix it. I modified the PS1 variable to show the current branch! Here’s what I added to the .bashrc file in Ubuntu :)

function parse_git_branch {
    ref=$(git symbolic-ref HEAD 2> /dev/null) || return
    echo "("${ref#refs/heads/}")"
}

RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"

PS1="$PS1$RED\$(date +%H:%M) \w$YELLOW \$(parse_git_branch)$GREEN\$\[\033[0m\] "