Add colored git branch name to your shell prompt
Here’s a little trick to get the name of your current git branch in your shell prompt. No more figuring out that you have made changes to the wrong branch once it’s too late! Your shell will not change for normal (non-git) folders. But, once you change directory into a git project, you will see the branch name added to your shell prompt like such:
Macintosh-2:git_project_foo andyuser [master] $
Simply add the following to your ~/.bash_profile or ~/.bashrc file:
function parse_git_branch_and_add_brackets {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\ \[\1\]/'
}
PS1="\h:\W \u\[\033[0;32m\]\$(parse_git_branch_and_add_brackets) \[\033[0m\]\$ "
That’s all there is to it ! Try it out !

git 1.7.0.4+ already has a function defined for this called __git_ps1. But the solution below has no colour:
The \$ caused a problem in my bash, i had to remove the \
Code:
case “$TERM” in
xterm*|rxvt*)
PS1=’\u@\h:\w$(__git_ps1 “[%s]“)$ ‘
;;
*)
;;
esac
jol blazey
22 Jan 12 at 3:21 pm