Skip to content

Instantly share code, notes, and snippets.

@Caleb9
Created June 6, 2020 14:10
Show Gist options
  • Save Caleb9/e30eec4b527aad51ee9d3a2d7caef985 to your computer and use it in GitHub Desktop.
Save Caleb9/e30eec4b527aad51ee9d3a2d7caef985 to your computer and use it in GitHub Desktop.
Git branch with status color in Bash prompt
# Git branch status
# Modified from https://coderwall.com/p/pn8f0g/show-your-git-status-and-branch\-in-color-at-the-command-prompt
function git_color {
local color_red="\e[31m"
local color_green="\e[1;32m"
local color_yellow="\e[1;33m"
local color_ochre="\e[38;5;95m"
local git_status="$(git status 2> /dev/null)"
if [[ ! $git_status =~ "working tree clean" ]]; then
echo -e $color_red
elif [[ $git_status =~ "Your branch is ahead of" ]]; then
echo -e $color_yellow
elif [[ $git_status =~ "nothing to commit" ]]; then
echo -e $color_green
else
echo -e $color_ochre
fi
}
# Add colored branch name
PS1+='\[$(git_color)\]$(__git_ps1 "[%s] ")'
# Break line
PS1+='\n'
# Color '$' and reset color
PS1+='\[\e[37m\]$ \[\e[m\]'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment