This is an old revision of the document!


Setting up the prompt to show git branch and state information

A small snippet of code is run every time the prompt is about to be printed, that outputs information about the current branch, and staged and unstaged modifications.

Put the following in your ~/.bashrc file:

for candidate in /usr/share/git-core/contrib/completion/git-prompt.sh /usr/lib/git-core/git-sh-prompt; do
    ! type __git_ps1 &>/dev/null && [ -e $candidate ] && . $candidate
done
 
if type __git_ps1 &>/dev/null; then
    export GIT_PS1_SHOWDIRTYSTATE=1
    export GIT_PS1_SHOWUNTRACKEDFILES=1
    export GIT_PS1_SHOWCOLORHINTS=1
    export PROMPT_DIRTRIM=2
    export PROMPT_COMMAND='__git_ps1 "\w" "\\\$ "'
fi

… and then reload your shell config with exec bash.

Edit `~/.bash_profile`

if [ -f $(brew --prefix)/etc/bash_completion.d/git-prompt.sh ]; then
     . $(brew --prefix)/etc/bash_completion.d/git-prompt.sh        # This path might need to be adjusted
fi
 
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWSTASHSTATE=1
export GIT_PS1_SHOWUNTRACKEDFILES=1
# export GIT_PS1_SHOWUPSTREAM=verbose
# export GIT_PS1_DESCRIBE_STYLE=branch
export PROMPT_COMMAND='__git_ps1 "\u@\h:\w" " \\\$ "'
export GIT_PS1_SHOWCOLORHINTS=1

(Works only with the homebrew version of git.)

Under both Linux and Mac, if you cannot find git-prompt.sh, just get the latest version from git git https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh, save it anywhere in the home directory, and put the path to it in the line annotated with “This path might need to be adjusted”.