pn8f0g
Last Updated: April 30, 2017
·
32.47K
· jkrmr
Oz64b0i9

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

Say Thanks
Respond

9 Responses
Add your response

11270
3eae340e049abecfb4a117ad7a907bd1

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

over 1 year ago ·
11336
538980bfd30153e43d8ffc3357b652ad

Thank you.
It is helpful to me :)

over 1 year ago ·
11796
A7388563d1e8dae1d5ff1ee683cc72ea

yeah, pretty cool hack. I like it.

over 1 year ago ·
11911
Bwoston bit headshot

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 ·
12271
237cb96458a3a593e8f99a46fe4b712e

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

[color]
    ui = true
over 1 year ago ·
17753
None

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

over 1 year ago ·
18116
None

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 ·
26349
None

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 ·
27788

`

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 ·
Filed Under