Skip to content

Instantly share code, notes, and snippets.

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 che-wf/0fd26279ec483e67fe836e7d9af9180e to your computer and use it in GitHub Desktop.
Save che-wf/0fd26279ec483e67fe836e7d9af9180e to your computer and use it in GitHub Desktop.
Code for your .bashrc file. Show current git branch on bash shell prompt....with color! (This was copied and modified from similar code you can find out there on the 'tubes.)
# Colors for the prompt
blue="\033[0;34m"
white="\033[0;37m"
green="\033[0;32m"
# Brackets needed around non-printable characters in PS1
ps1_blue='\['"$blue"'\]'
ps1_green='\['"$green"'\]'
ps1_white='\['"$white"'\]'
parse_git_branch() {
gitstatus=`git status 2> /dev/null`
if [[ `echo $gitstatus | grep "Changes to be committed"` != "" ]]
then
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1***)/'
elif [[ `echo $gitstatus | grep "Changes not staged for commit"` != "" ]]
then
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1**)/'
elif [[ `echo $gitstatus | grep "Untracked"` != "" ]]
then
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1*)/'
elif [[ `echo $gitstatus | grep "nothing to commit"` != "" ]]
then
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
else
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1?)/'
fi
}
# Echo a non-printing color character depending on whether or not the current git branch is the master
# Does NOT print the branch name
# Use the parse_git_branch() function for that.
parse_git_branch_color() {
br=$(parse_git_branch)
if [[ $br == "(master)" || $br == "(master*)" || $br == "(master**)" || $br == "(master***)" ]]; then
echo -e "${blue}"
else
echo -e "${green}"
fi
}
# No color:
#export PS1="@\h:\W\$(parse_git_branch) \$ "
# With color:
export PS1="$ps1_blue@\h:$ps1_white\W\[\$(parse_git_branch_color)\]\$(parse_git_branch) \n $ps1_blue\$$ps1_white "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment