Last Updated: October 04, 2020
·
80.04K
· jkrmr

Show your git status and branch (in color) at the command prompt

I'm a huge fan of having the branch and status for my current project reflected in my bash prompt. Here's what mine looks like:

command prompt with git status

And here's how to get that:

First define some colors. This will make it easier to work with the escape sequences later:

COLOR_RED="\033[0;31m"
COLOR_YELLOW="\033[0;33m"
COLOR_GREEN="\033[0;32m"
COLOR_OCHRE="\033[38;5;95m"
COLOR_BLUE="\033[0;34m"
COLOR_WHITE="\033[0;37m"
COLOR_RESET="\033[0m"

Next, a function for the color formatting:

function git_color {
  local git_status="$(git status 2> /dev/null)"

  if [[ ! $git_status =~ "working directory clean" ]]; then
    echo -e $COLOR_RED
  elif [[ $git_status =~ "Your branch is ahead of" ]]; then
    echo -e $COLOR_YELLOW
  elif [[ $git_status =~ "nothing to commit" ]]; then
    echo -e $COLOR_GREEN
  else
    echo -e $COLOR_OCHRE
  fi
}

and one for the git branch:

function git_branch {
  local git_status="$(git status 2> /dev/null)"
  local on_branch="On branch ([^${IFS}]*)"
  local on_commit="HEAD detached at ([^${IFS}]*)"

  if [[ $git_status =~ $on_branch ]]; then
    local branch=${BASH_REMATCH[1]}
    echo "($branch)"
  elif [[ $git_status =~ $on_commit ]]; then
    local commit=${BASH_REMATCH[1]}
    echo "($commit)"
  fi
}

NB: The formatting of git status messages has changed, so if you're on the latest version of git, you'll likely need to use"^On branch instead of "^# On branch above. Check your git status to be sure you're using the right string.

Lastly, incorporate into the PS1 declaration (broken up for clarity):

PS1="\[$COLOR_WHITE\]\n[\W]"          # basename of pwd
PS1+="\[\$(git_color)\]"        # colors git status
PS1+="\$(git_branch)"           # prints current branch
PS1+="\[$COLOR_BLUE\]\$\[$COLOR_RESET\] "   # '#' for root, else '$'
export PS1

Voila!

reference: The Bash Prompt HOWTO

Related protips:

Remove all your local git branches but keep master

8 Responses
Add your response

Or you have vcprompt already managing all of that for you in portable manner (bash, zsh, …) without reinventing the wheel ;)

over 1 year ago ·

Thank you.
It is helpful to me :)

over 1 year ago ·

For fish users, you can run the fish_config command and then select from several git branch prompt options in the GUI. It also prints out the relevant code right there so that you can easily tweak/modify in your config.fish file.

— "But nobody uses fish!"

— "I do! :( "

over 1 year ago ·

Also if you want some color in the git messages, add this to your .gitconfig.

[color]
    ui = true
over 1 year ago ·

I think you have a typo in the git_color function. The last elif should be an else right?

over 1 year ago ·

At least on my version of bash in Ubuntu 14.04, the last elif in git_color() needs to be a straight else since it's not testing any condition.

elif   # should be else
  echo -e $COLOR_OCHRE
over 1 year ago ·

Colors in PS1 command needs to match the defined ones.

PS1="[$COLOR_WHITE]\n[\W]" # basename of pwd

PS1+="[\$(git_color)]" # colors git status

PS1+="\$(git_branch)" # prints current branch

PS1+="[$COLORYELLOW]\$[$COLORRESET] " # '#' for root, else '$'

export PS1

over 1 year ago ·

`

For git branch

COLORRED="\033[0;31m"
COLOR
YELLOW="\033[0;33m"
COLORGREEN="\033[0;32m"
COLOR
OCHRE="\033[38;5;95m"
COLORBLUE="\033[0;34m"
COLOR
WHITE="\033[0;37m"
COLOR_RESET="\033[0m"

Next, a function for the color formatting:

function gitcolor {
local git
status="$(git status 2> /dev/null)"

if [[ ! $gitstatus =~ "working directory clean" ]]; then
echo -e $COLOR
RED
elif [[ $gitstatus =~ "Your branch is ahead of" ]]; then
echo -e $COLOR
YELLOW
elif [[ $gitstatus =~ "nothing to commit" ]]; then
echo -e $COLOR
GREEN
else
echo -e $COLOR_OCHRE
fi
}

and one for the git branch:

function gitbranch {
local git
status="$(git status 2> /dev/null)"
local onbranch="On branch ([^${IFS}]*)"
local on
commit="HEAD detached at ([^${IFS}]*)"

if [[ $gitstatus =~ $onbranch ]]; then
local branch=${BASHREMATCH[1]}
echo "($branch)"
elif [[ $git
status =~ $oncommit ]]; then
local commit=${BASH
REMATCH[1]}
echo "($commit)"
fi
}

PS1="[$COLORWHITE]\W:" # basename of pwd
PS1+="[\$(git
color)]" # colors git status
PS1+="\$(gitbranch)" # prints current branch
PS1+="[$COLOR
BLUE]\$[$COLOR_RESET] " # '#' for root, else '$'
export PS1
`

over 1 year ago ·