Displaying git branch on terminal prompt

Displaying git branch on terminal prompt

Put the following in ~/.bashrc file:

function git-branch-name { 
    git branch 2>/dev/null | grep '^*' | colrm 1 2
}
function git-branch-prompt { 
    local branch=`git-branch-name`
    if [ $branch ]; then printf " [%s]" $branch; fi
}
PS1="\u@\h \[\033[0;36m\]\w\[\033[0m\]\[\033[0;32m\]\$(git-branch-prompt)\[\033[0m\] \$"

Source the file:

source ~/.bashrc

Alternate:

export PS1='\[\033[01;32m\]\[\033[0m\033[0;32m\]\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1)$ '

If you see error, then probably `bash-completion` package is not installed. In debian based systems you can use the following command to install bash-completion

sudo apt install bash-completion

Source: https://gist.github.com/justintv/168835