-
-
Save paulredmond/3130269 to your computer and use it in GitHub Desktop.
bashrc with git ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WHITE="\[\033[00m\]" | |
BLACK="\[\033[01;30m\]" | |
RED="\[\033[0;31m\]" | |
LIGHT_RED="\[\033[1;31m\]" | |
GREEN="\[\033[0;32m\]" | |
LIGHT_GREEN="\[\033[1;32m\]" | |
YELLOW="\[\033[0;33m\]" | |
BLUE="\[\033[0;34m\]" | |
PURPLE="\[\033[0;35m\]" | |
CYAN="\[\033[0;36m\]" | |
LIGHT_GRAY="\[\033[0;37m\]" | |
function parse_git_branch { | |
git rev-parse --git-dir > /dev/null 2>&1 | |
if [ $? -eq 0 ]; then | |
git_status="$(git status 2> /dev/null)" | |
branch_pattern="^# On branch ([^${IFS}]*)" | |
detached_branch_pattern="# Not currently on any branch" | |
remote_pattern="# Your branch is (.*) of" | |
diverge_pattern="# Your branch and (.*) have diverged" | |
untracked_pattern="# Untracked files:" | |
new_pattern="new file:" | |
not_staged_pattern="Changes not staged for commit" | |
#files not staged for commit | |
if [[ ${git_status}} =~ ${not_staged_pattern} ]]; then | |
state="✔" | |
fi | |
# add an else if or two here if you want to get more specific | |
# show if we're ahead or behind HEAD | |
if [[ ${git_status} =~ ${remote_pattern} ]]; then | |
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then | |
remote="↑" | |
else | |
remote="↓" | |
fi | |
fi | |
#new files | |
if [[ ${git_status} =~ ${new_pattern} ]]; then | |
remote="+" | |
fi | |
#untracked files | |
if [[ ${git_status} =~ ${untracked_pattern} ]]; then | |
remote="✖" | |
fi | |
#diverged branch | |
if [[ ${git_status} =~ ${diverge_pattern} ]]; then | |
remote="↕" | |
fi | |
#branch name | |
if [[ ${git_status} =~ ${branch_pattern} ]]; then | |
branch=${BASH_REMATCH[1]} | |
#detached branch | |
elif [[ ${git_status} =~ ${detached_branch_pattern} ]]; then | |
branch="NO BRANCH" | |
fi | |
echo " ( ${branch} ${state}${remote})" | |
fi | |
return | |
} | |
export PS1="$PURPLE\u@\h$WHITE:$BLUE\w$YELLOW\$(parse_git_branch)$WHITE\$ " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment