Tip: Have Git Branch Displayed in Bash Prompt

This is a very small tip, but one that can really save accidents in the long term.

To display the current branch in git simply add the following to your .bashrc in your home directory :

__git_ps1 () 
{ 
    local b="$(git symbolic-ref HEAD 2>/dev/null)";
    if [ -n "$b" ]; then
        printf " (%s)" "${b##refs/heads/}";
    fi
}

PS1="${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w\$( __git_ps1 ) \$\[\033[00m\] "

This is based on the Linux Mint prompt so is nicely coloured as well.


Tags: ubuntulinuxdevelopergitbashtip