Skip to content

Instantly share code, notes, and snippets.

@BielStela
Last active April 13, 2018 15:40
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 BielStela/37aa5bb9319083fd47603abe80d8ffa7 to your computer and use it in GitHub Desktop.
Save BielStela/37aa5bb9319083fd47603abe80d8ffa7 to your computer and use it in GitHub Desktop.
Git status on bash prompt
# PS1 customization
NC='\001\033[0m\002'
RED='\001\033[00;31m\002'
GREEN='\001\033[00;32m\002'
YELLOW='\001\033[00;33m\002'
BLUE='\001\033[00;34m\002'
PURPLE='\001\033[00;35m\002'
CYAN='\001\033[00;36m\002'
WHITE='\001\033[00;37m\002'
LRED='\001\033[01;31m\002'
LGREEN='\001\033[01;32m\002'
LYELLOW='\001\033[01;33m\002'
LBLUE='\001\033[01;34m\002'
LPURPLE='\001\033[01;35m\002'
LCYAN='\001\033[01;36m\002'
WHITE2='\001\033[01;37m\002'
# get current branch in git repo
function parse_git_branch() {
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
if [ ! "${BRANCH}" == "" ]
then
STAT=`parse_git_dirty`
echo -en "${WHITE}[${BRANCH}${NC}${STAT}${WHITE}]${NC}"
else
echo ""
fi
}
# get current status of git repo
function parse_git_dirty {
status=`git status 2>&1 | tee`
dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
bits=''
if [ "${renamed}" == "0" ]; then
bits="\e[33m>\e[m${bits}"
fi
if [ "${ahead}" == "0" ]; then # magenta
bits="${PURPLE}**${NC}${bits}"
fi
if [ "${newfile}" == "0" ]; then # green
bits="${GREEN}+${NC}${bits}"
fi
if [ "${untracked}" == "0" ]; then # cyan
bits="${CYAN}?${NC}${bits}"
fi
if [ "${deleted}" == "0" ]; then # red
bits="${RED}x${NC}${bits}"
fi
if [ "${dirty}" == "0" ]; then
bits="${LGREEN}!${NC}${bits}"
fi
if [ ! "${bits}" == "" ]; then
echo -en " ${bits}"
else
echo ""
fi
}
# prompt like:
# user@host /absolute/path [branch **]$
# change de configuration and color as you want
export PS1="${GREEN}\u@\h${NC} ${BLUE}\w${NC} \`parse_git_branch\`${BLUE}\\$ ${NC}"
@BielStela
Copy link
Author

At this to ~/.bashrc and do >$ source ~/.bashrc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment