Adding GIT branch to BASH prompt

Save this:
https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh to ~/.git-prompt.sh

Then add to ~/.bashrc:

source ~/.git-prompt.sh
GIT_PS1_SHOWCOLORHINTS=1
PROMPT_COMMAND='__git_ps1 "\# \w" ": "'

yielding:
19 ~/proj/app (develop):

Note:
There are more options to show the status of files. See the .git-prompt.sh for info.

Edit:
This is a combined Mercurial / Git version:

hg_ps1() {
    hg prompt "({branch})" 2> /dev/null
}
#PS1='\# $(hg_ps1) \w: '
source ~/.git-prompt.sh
GIT_PS1_SHOWCOLORHINTS=1
PROMPT_COMMAND='__git_ps1 "\#" " $(hg_ps1) \w: "'

This entry was posted in programming. Bookmark the permalink.

Leave a Reply

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