Displaying Git Branch Info in the Bash Prompt

written in other

Assuming git is installed at /usr/local/git, then the following can be added to your .bash_profile to display info on the current branch with the included git-prompt.sh:

1
2
3
4
5
6
if [ -f /usr/local/git/contrib/completion/git-prompt.sh ]; then
    GIT_PS1_SHOWUPSTREAM="auto"
    GIT_PS1_SHOWCOLORHINTS="yes"
    source /usr/local/git/contrib/completion/git-prompt.sh
    export PROMPT_COMMAND='__git_ps1 "\u:\W" "\\\$ ";'
fi

Simply vi ~/.bash_profile, paste the above snippet, and save. And don’t forget to reload your profile using: source ~/.bash_profile. After applying the changes, your prompt will resemble: danielstjules:Markus (machinist2 =)$

That consists of my username, the basename of the working dir, the branch name, as well as its status compared to upstream. That said, I personally like highlighting my username and seeing the path to the working directory, so I use the following:

1
export PROMPT_COMMAND='__git_ps1 "\[`[ $? = 0 ] && X=2 || X=1; tput setaf $X`\]\u\[`tput sgr0`\]:\w" "\n\\\$ ";'

Preview of my shell


Comments