Skip to content

Instantly share code, notes, and snippets.

@fredjoseph
Last active June 3, 2019 08:03
Show Gist options
  • Save fredjoseph/365f8c4f725a40c2522651bf8eb6e73d to your computer and use it in GitHub Desktop.
Save fredjoseph/365f8c4f725a40c2522651bf8eb6e73d to your computer and use it in GitHub Desktop.
[Git Bash] Custom Prompt

The goal is to display the status of a git repository directly in the prompt.

Solution 1

  • Update the .bashrc file with the following lines
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWUNTRACKEDFILES=1
export GIT_PS1_SHOWUPSTREAM="auto verbose"
export GIT_PS1_SHOWCOLORHINTS=1
export GIT_PS1_SHOWSTASHSTATE=1
export PROMPT_COMMAND='__git_ps1 "\[\033[33m\]\w\[\033[0m\]" "\n\\\$ "'

This solution displays many information but is slow (adding some seconds for displaying the prompt)

Solution 2

  • Update the .bashrc file with the following lines
git_color() {
  [[ -n $(git status --porcelain=v2 2>/dev/null) ]] && echo 31 || echo 32
}

export PS1='\[\033[33m\]\w\[\033[`git_color`m\]`__git_ps1`\[\033[0m\]\n$ '

This solution keeps unchanged the information displayed, but changes the color (green if status is clean, otherwise red)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment