How to see git/mercurial branch on your command line

Add this to your `~/.bashrc` file:


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

function hg_dirty() {
hg status --no-color 2> /dev/null \
| awk '$1 == "?" { unknown = 1 }
$1 != "?" { changed = 1 }
END {
if (changed) printf "!"
else if (unknown) printf "?"
}'
}

function hg_branch() {
hg branch 2> /dev/null | awk '{ printf " (" $1 ")" }'
hg bookmarks -a 2> /dev/null | awk '/\*/ { printf " (" $2 ")"}'
}

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

DEFAULT="[37;40m"
PINK="[35;40m"
RANGE="[33;40m"

PS1="$GREEN\u@\h$NO_COLOR:\w$YELLOW\$(parse_git_branch)$YELLOW\$(hg_branch)$NO_COLOR\$ "

Leave a Reply

Your email address will not be published. Required fields are marked *