Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git prompt
DEFAULT="\[\033[1;00m\]"
BLACK="\[\033[1;30m\]"
YELLOW="\[\033[1;33m\]"
GREEN="\[\033[1;32m\]"
WHITE="\[\033[1;37m\]"
BLUE="\[\033[1;36m\]"
# Returns "*" if the current git branch is dirty.
function parse_git_dirty {
[[ $(git diff --shortstat 2> /dev/null | tail -n1) != "" ]] && echo "*"
}
# Returns "|shashed:N" where N is the number of stashed states (if any).
function parse_git_stash {
local stash=`expr $(git stash list 2>/dev/null| wc -l)`
if [ "$stash" != "0" ]
then
echo "|stashed:$stash"
fi
}
# Get the current git branch name (if available)
git_prompt() {
local ref=$(git symbolic-ref HEAD 2>/dev/null | cut -d'/' -f3)
if [ "$ref" != "" ]
then
echo "($ref$(parse_git_dirty)$(parse_git_stash)) "
fi
}
export PS1="[$GREEN\u:$YELLOW\w$DEFAULT]$BLUE \$(git_prompt)\$ $DEFAULT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.