Skip to content

Instantly share code, notes, and snippets.

@Shemahmforash
Forked from tung/gist:409780
Last active August 29, 2015 14:19
Show Gist options
  • Save Shemahmforash/5f5822a16879f378328d to your computer and use it in GitHub Desktop.
Save Shemahmforash/5f5822a16879f378328d to your computer and use it in GitHub Desktop.
Git branch information (as well as status comparing with the remote) in bash prompt
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[0;34m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
WHITE="\[\033[1;37m\]"
LIGHT_GRAY="\[\033[0;37m\]"
COLOR_NONE="\[\e[0m\]"
parse_git_branch() {
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"
pattern="^(.*)working directory clean"
if [[ ! ${git_status} =~ $pattern ]]; then
state="${LIGHT_RED}⚡"
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="${YELLOW}↑"
else
remote="${YELLOW}↓"
fi
fi
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="${YELLOW}↕"
fi
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
echo " (${branch})${remote}${state}"
fi
}
function prompt_func() {
previous_return_value=$?;
if [ "$TERM" != "linux" -a -z "$EMACS" ]
then
TITLEBAR="\[\e]2;\u@\h:\w\a\]"
else
TITLEBAR=""
fi
prompt="${TITLEBAR}${BLUE}[${YELLOW}\u@\h${RED} \w${GREEN}$(parse_git_branch)${BLUE}]${COLOR_NONE}"
if test $previous_return_value -eq 0
then
PS1="${prompt}${GREEN}\\\$${COLOR_NONE} "
else
PS1="${prompt}${LIGHT_RED}\\\$${COLOR_NONE} "
fi
}
PROMPT_COMMAND=prompt_func
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment