How to display the Mercurial branch name to bash prompt

To have your current branch name in your bash prompt, just add this code into .bash_profile file from your home directory.

hgBranch ()
{
if [ -d .hg ]; then
    printf "$1" "$(hg branch)"
fi
}
 
export PS1='${USER}@${HOSTNAME}:\w$(hgBranch " [\033[1;31m(%s)\033[0m]")$ '

Ofcourse, after you edit .bash_profile, you must logout and login again to see the changes.

//later edit

If you want to have the same feature in the graphical terminal, you should add the code above in the .bashrc file.

//2nd later edit
You can find a good “how to” for git on LeaseWeb Labs.