Skip to content

Instantly share code, notes, and snippets.

@magoon
Last active December 19, 2015 18:58
Show Gist options
  • Save magoon/6002421 to your computer and use it in GitHub Desktop.
Save magoon/6002421 to your computer and use it in GitHub Desktop.
Colored git prompt (current branch, status, and stash count)
################################################
# Git prompt
#
# 2013-06-25 amagoon@northps.com: added statuses
#
# save this as ~/.bash_ps1
#
# point to this file in your ~/.bash_profile
# e.g.:
#
# # Git prompt
# if [ -f "$HOME/.bash_ps1" ]; then
# . "$HOME/.bash_ps1"
# fi
#
# After (branch name), you'll see one or more of
# + uncommitted changes
# * unstaged changes
# _ untracked files
# 1 number of stashes for this branch
#
################################################
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
BRIGHTGREEN="\[\033[1;32m\]"
BLUE="\[\033[1;34m\]"
MAGENTA="\[\033[0;35m\]"
NO_COLOUR="\[\033[0m\]"
PROMPT_CHAR='$ '
# Change git color if branch is dirty
function parse_git_staged() {
local git_status="`git status -unormal 2>&1`"
if [[ "$git_status" =~ Changes\ to\ be\ committed ]] ; then
echo -n "+"
fi
}
function parse_git_unstaged() {
local git_status="`git status -unormal 2>&1`"
if [[ "$git_status" =~ Changes\ not\ staged\ for\ commit ]] ; then
echo -n "*"
fi
}
function parse_git_untracked() {
local git_status="`git status -unormal 2>&1`"
if [[ "$git_status" =~ Untracked\ files ]] ; then
echo -n "_"
fi
}
# Show the branch name in parens
function parse_git_branch () {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
# Show count of stashes for this branch
function parse_git_stashes () {
git stash list 2> /dev/null | grep "n `git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`:" | wc -l | sed 's/^ *//' | sed 's/ *$//' | sed 's/^0$//'
}
PS1="$GREEN\u@\h$NO_COLOUR:\w$GREEN\$(parse_git_branch)$BRIGHTGREEN\$(parse_git_staged)$YELLOW\$(parse_git_unstaged)$RED\$(parse_git_untracked)$BLUE\$(parse_git_stashes)$NO_COLOUR ${PROMPT_CHAR} "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment