Skip to content

Instantly share code, notes, and snippets.

@rschenk
Created October 17, 2012 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rschenk/3907556 to your computer and use it in GitHub Desktop.
Save rschenk/3907556 to your computer and use it in GitHub Desktop.
Add git information to bash prompt
# Bedazzle terminal with Git branch information
NO_COLOR="\[\033[0m\]"
BLACK="\[\033[0;30m\]"
RED="\[\033[0;31m\]"
GREEN="\[\033[0;32m\]"
YELLOW="\[\033[0;33m\]"
BLUE="\[\033[0;34m\]"
PURPLE="\[\033[0;35m\]"
CYAN="\[\033[0;36m\]"
GRAY="\[\033[0;90m\]"
LIGHT_GRAY="\[\033[0;37m\]"
LIGHT_RED="\[\033[0;91m\]"
LIGHT_GREEN="\[\033[0;92m\]"
LIGHT_YELLOW="\[\033[0;93m\]"
LIGHT_BLUE="\[\033[0;94m\]"
LIGHT_PURPLE="\[\033[0;95m\]"
LIGHT_CYAN="\[\033[0;96m\]"
WHITE="\[\033[0;97m\]"
function parse_git_branch {
git rev-parse --git-dir &> /dev/null
git_status="$(git status 2> /dev/null)"
branch_pattern="^# On branch ([^${IFS}]*)"
remote_pattern="# Your branch is (.*) of"
diverge_pattern="# Your branch and (.*) have diverged"
if [[ ! ${git_status} =~ "working directory clean" ]]; then
state="${LIGHT_PURPLE}⚡"
fi
# add an else if or two here if you want to get more specific
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
remote="${LIGHT_YELLOW}↑"
else
remote="${LIGHT_YELLOW}↓"
fi
fi
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="${LIGHT_YELLOW}↕"
fi
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
echo " (${branch})${remote}${state}"
fi
}
function prompt_func() {
previous_return_value=$?;
prompt="${NO_COLOR}${NO_COLOR}\W${GRAY}$(parse_git_branch)${NO_COLOR}"
if test $previous_return_value -eq 0
then
PS1="${prompt}${GREEN} $ ${NO_COLOR}"
else
PS1="${prompt}${RED} $ ${NO_COLOR}"
fi
}
PROMPT_COMMAND=prompt_func
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment