Skip to content
andersk edited this page Sep 23, 2014 · 1 revision

Git bash prompt

Distributed as part of the Git bash completion hook, the __git_ps1 function shows handy information about the current Git state in your prompt whenever you’re inside a Git checkout. For example:

user@hostname:~/dir (master *>)$ 

indicates that you’re on the master branch with unstaged changes and also unpushed commits. Here are the meanings of various characters that might appear, and the configuration options that control them:

  • * There are unstaged changes (shown with GIT_PS1_SHOWDIRTYSTATE).
  • + There are staged but uncommitted changes (shown with GIT_PS1_SHOWDIRTYSTATE).
  • $ There is a stash (shown with GIT_PS1_SHOWSTASHSTATE).
  • % There are untracked files (shown with GIT_PS1_SHOWUNTRACKEDFILES—warning, this might slow down your prompt).
  • > There are local commits on HEAD that have not been pushed to its upstream branch (shown with GIT_PS1_SHOWUPSTREAM).
  • < There are commits on the upstream branch that have not been merged to HEAD (shown with GIT_PS1_SHOWUPSTREAM).
  • <> Both, i.e., HEAD and its upstream have diverged (shown with GIT_PS1_SHOWUPSTREAM).
  • = HEAD points to the same commit as its upstream (shown with GIT_PS1_SHOWUPSTREAM).

The ~/.bashrc snippets below will load the Git bash prompt and insert Git state right before the last \$ in your PS1. The first line may not be needed; most distros load __git_ps1 by default if bash completion is enabled. Modify as appropriate if your git-prompt.sh is not installed at $(git --exec-path)/git-sh-prompt or if your PS1 does not use \$.

Colored (requires Git ≥ 1.8.1)

. "$(git --exec-path)/git-sh-prompt"
PROMPT_COMMAND="$(printf '%q ' __git_ps1 "${PS1%%\\\$*}" "\\\$${PS1#*\\\$}");$PROMPT_COMMAND"
GIT_PS1_SHOWCOLORHINTS=t
GIT_PS1_SHOWDIRTYSTATE=t
GIT_PS1_SHOWSTASHSTATE=t
#GIT_PS1_SHOWUNTRACKEDFILES=t
GIT_PS1_SHOWUPSTREAM=git

Uncolored

. "$(git --exec-path)/git-sh-prompt"
PS1="${PS1/\\\$/\$(__git_ps1)\\\$}"
GIT_PS1_SHOWDIRTYSTATE=t
GIT_PS1_SHOWSTASHSTATE=t
#GIT_PS1_SHOWUNTRACKEDFILES=t
GIT_PS1_SHOWUPSTREAM=git

More options

See the comments at the top of git-prompt.sh.

Clone this wiki locally