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:
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:
Written by Jake Romer
Related protips
8 Responses
Or you have vcprompt
already managing all of that for you in portable manner (bash
, zsh
, …) without reinventing the wheel ;)
Thank you.
It is helpful to me :)
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! :( "
Also if you want some color in the git messages, add this to your .gitconfig
.
[color]
ui = true
I think you have a typo in the git_color function. The last elif should be an else right?
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
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
`
For git branch
COLORRED="\033[0;31m"
COLORYELLOW="\033[0;33m"
COLORGREEN="\033[0;32m"
COLOROCHRE="\033[38;5;95m"
COLORBLUE="\033[0;34m"
COLORWHITE="\033[0;37m"
COLOR_RESET="\033[0m"
Next, a function for the color formatting:
function gitcolor {
local gitstatus="$(git status 2> /dev/null)"
if [[ ! $gitstatus =~ "working directory clean" ]]; then
echo -e $COLORRED
elif [[ $gitstatus =~ "Your branch is ahead of" ]]; then
echo -e $COLORYELLOW
elif [[ $gitstatus =~ "nothing to commit" ]]; then
echo -e $COLORGREEN
else
echo -e $COLOR_OCHRE
fi
}
and one for the git branch:
function gitbranch {
local gitstatus="$(git status 2> /dev/null)"
local onbranch="On branch ([^${IFS}]*)"
local oncommit="HEAD detached at ([^${IFS}]*)"
if [[ $gitstatus =~ $onbranch ]]; then
local branch=${BASHREMATCH[1]}
echo "($branch)"
elif [[ $gitstatus =~ $oncommit ]]; then
local commit=${BASHREMATCH[1]}
echo "($commit)"
fi
}
PS1="[$COLORWHITE]\W:" # basename of pwd
PS1+="[\$(gitcolor)]" # colors git status
PS1+="\$(gitbranch)" # prints current branch
PS1+="[$COLORBLUE]\$[$COLOR_RESET] " # '#' for root, else '$'
export PS1
`