Skip to content

Instantly share code, notes, and snippets.

@zanshin
Created January 5, 2016 17:14
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 zanshin/c738acfac9c435095951 to your computer and use it in GitHub Desktop.
Save zanshin/c738acfac9c435095951 to your computer and use it in GitHub Desktop.
Bash prompt showing Git status
# Import some colors
source ~/.dotfiles/bash/colors
# Variables used in building the Git status portiion of the prompt
GIT_PROMPT_SYMBOL="${Blue}±"
GIT_PROMPT_PREFIX="${Green} [${Color_Off}"
GIT_PROMPT_SUFFIX="${Green}]${Color_Off}"
GIT_PROMPT_AHEAD="${BRed}ANUM${Color_Off}"
GIT_PROMPT_BEHIND="${Cyan}BNUM${Color_Off}"
GIT_PROMPT_MERGING="${Red}⚡︎${Color_Off}"
GIT_PROMPT_UNTRACKED="${BRed}u${Color_Off}"
GIT_PROMPT_MODIFIED="${Yellow}d${Color_Off}"
GIT_PROMPT_STAGED="${Green}s${Color_Off}"
# Show Git branch/tag, or name-rev if on detached head
function parse_git_branch() {
(git name-rev --name-only --no-undefined --always HEAD || git symbolic-ref -q HEAD) 2> /dev/null
}
# Show different symbols as appropriate for various Git repository states
function parse_git_state() {
# Compose this value via multiple conditional appends.
local GIT_STATE=""
local NUM_AHEAD="$(git log --oneline @{u}.. 2> /dev/null | wc -l | tr -d ' ')"
if [ "$NUM_AHEAD" -gt 0 ]; then
GIT_STATE=$GIT_STATE${GIT_PROMPT_AHEAD//NUM/$NUM_AHEAD}
fi
local NUM_BEHIND="$(git log --oneline ..@{u} 2> /dev/null | wc -l | tr -d ' ')"
if [ "$NUM_BEHIND" -gt 0 ]; then
GIT_STATE=$GIT_STATE${GIT_PROMPT_BEHIND//NUM/$NUM_BEHIND}
fi
local GIT_DIR="$(git rev-parse --git-dir 2> /dev/null)"
if [ -n $GIT_DIR ] && test -r $GIT_DIR/MERGE_HEAD; then
GIT_STATE=$GIT_STATE$GIT_PROMPT_MERGING
fi
if [[ -n $(git ls-files --other --exclude-standard 2> /dev/null) ]]; then
GIT_STATE=$GIT_STATE$GIT_PROMPT_UNTRACKED
fi
if ! git diff --quiet 2> /dev/null; then
GIT_STATE=$GIT_STATE$GIT_PROMPT_MODIFIED
fi
if ! git diff --cached --quiet 2> /dev/null; then
GIT_STATE=$GIT_STATE$GIT_PROMPT_STAGED
fi
if [[ -n $GIT_STATE ]]; then
echo "$GIT_PROMPT_PREFIX$GIT_STATE$GIT_PROMPT_SUFFIX"
fi
}
# If inside a Git repository, print its branch and state
function git_prompt_string() {
local git_where="$(parse_git_branch)"
[ -n "$git_where" ] && echo "on ${Green}${git_where#(refs/heads/|tags/)}${Color_Off}$(parse_git_state)"
}
# Use Bash's prompt_command feature to show the prompt. This causes the various functions the drive the prompt
# to be evaluated each time it is displayed. Using PS1 by itself introduces stale information.
function prompt_command {
export PS1="\n${Green}\u${Color_Off} at ${Blue}\H${Color_Off} in ${Yellow}\w${Color_Off} $(git_prompt_string)\n$ "
}
# Set the completed prompt
export PROMPT_COMMAND=prompt_command
# Reset
Color_Off='\e[0m' # Text Reset
# Regular Colors
Black='\e[0;30m' # Black
Red='\e[0;31m' # Red
Green='\e[0;32m' # Green
Yellow='\e[0;33m' # Yellow
Blue='\e[0;34m' # Blue
Purple='\e[0;35m' # Purple
Cyan='\e[0;36m' # Cyan
White='\e[0;37m' # White
# Bold
BBlack='\e[1;30m' # Black
BRed='\e[1;31m' # Red
BGreen='\e[1;32m' # Green
BYellow='\e[1;33m' # Yellow
BBlue='\e[1;34m' # Blue
BPurple='\e[1;35m' # Purple
BCyan='\e[1;36m' # Cyan
BWhite='\e[1;37m' # White
# Underline
UBlack='\e[4;30m' # Black
URed='\e[4;31m' # Red
UGreen='\e[4;32m' # Green
UYellow='\e[4;33m' # Yellow
UBlue='\e[4;34m' # Blue
UPurple='\e[4;35m' # Purple
UCyan='\e[4;36m' # Cyan
UWhite='\e[4;37m' # White
# Background
On_Black='\e[40m' # Black
On_Red='\e[41m' # Red
On_Green='\e[42m' # Green
On_Yellow='\e[43m' # Yellow
On_Blue='\e[44m' # Blue
On_Purple='\e[45m' # Purple
On_Cyan='\e[46m' # Cyan
On_White='\e[47m' # White
# High Intensity
IBlack='\e[0;90m' # Black
IRed='\e[0;91m' # Red
IGreen='\e[0;92m' # Green
IYellow='\e[0;93m' # Yellow
IBlue='\e[0;94m' # Blue
IPurple='\e[0;95m' # Purple
ICyan='\e[0;96m' # Cyan
IWhite='\e[0;97m' # White
# Bold High Intensity
BIBlack='\e[1;90m' # Black
BIRed='\e[1;91m' # Red
BIGreen='\e[1;92m' # Green
BIYellow='\e[1;93m' # Yellow
BIBlue='\e[1;94m' # Blue
BIPurple='\e[1;95m' # Purple
BICyan='\e[1;96m' # Cyan
BIWhite='\e[1;97m' # White
# High Intensity backgrounds
On_IBlack='\e[0;100m' # Black
On_IRed='\e[0;101m' # Red
On_IGreen='\e[0;102m' # Green
On_IYellow='\e[0;103m' # Yellow
On_IBlue='\e[0;104m' # Blue
On_IPurple='\e[0;105m' # Purple
On_ICyan='\e[0;106m' # Cyan
On_IWhite='\e[0;107m' # White
@zanshin
Copy link
Author

zanshin commented Jan 5, 2016

The .bashrc file show above is a partial file, showing the portion that sets up the Git status information and the prompt.

The resulting prompt will take up three lines: a blank separator line, a line showing the user, host, current directory, and git status (if in a git repository), and a final line with the $ prompt character.

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