28 August 2012

Git branch in prompt

Just a quick tip about how to display the git branch currently checked out when using the bash prompt. Put this little routine in your .bash_profile or similar:

# Git branch in prompt
parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

export PS1="\[\e]0;\h - \w\a\]\n\[\e[32m\]\u@\h \[\e[36m\]\$(parse_git_branch) \[\e[33m\]\w\[\e[0m\]\n\$ "

This will give you a prompt like this when you are standing in a git directory:

user@host (master) /cygdrive/c/repos/project/
In addition to being useful, it is ansi color coded so it will look good, too.

No comments:

Post a Comment