Skip to content

Instantly share code, notes, and snippets.

@joakime
Last active April 18, 2018 22:58
Show Gist options
  • Save joakime/2038606 to your computer and use it in GitHub Desktop.
Save joakime/2038606 to your computer and use it in GitHub Desktop.
Functions to present extra git info on bash prompt.
#!/bin/bash
#
# Shows git information in your bash prompt.
# Git Branch Name
# Git Dirty State (uncommitted changes)
# Git "Behind" Count (are you up to date?)
#
# =========================================
# To use:
#
# * Put this script into $HOME/bin
# * Add the following to your $HOME/.bashrc
#
# source $HOME/bin/git_prompt_functions.sh
# * Enjoy
# =========================================
# ANSI Background Codes
RED_BG="\[\033[41m\]"
BLUE_BG="\[\033[44m\]"
# ANSI Foreground Codes
RED="\[\033[31;1m\]"
GREEN="\[\033[32;1m\]"
YELLOW="\[\033[33;1m\]"
BLUE="\[\033[34;1m\]"
MAGENTA="\[\033[35;1m\]"
CYAN="\[\033[36;1m\]"
WHITE="\[\033[37;1m\]"
# ANSI Other Codes
ANSI_OFF="\[\033[0m\]"
parse_git_branch ()
{
BRANCH_ID=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
if [ -n "$BRANCH_ID" ] ; then
echo "[$BRANCH_ID$(parse_git_dirty)]"
fi
}
parse_git_behind ()
{
git branch --no-color -v 2> /dev/null | \
sed -e "/^[^*]/d" | \
grep --color=never -E " \[behind [0-9]*\] " | \
sed -e "s/.* \[behind \([0-9]*\)\].*/ -\1 /"
}
parse_git_dirty ()
{
git diff --quiet HEAD >&/dev/null;
[[ $? == 1 ]] && echo "*"
}
export PS1="${BLUE_BG}${BLUE}[${WHITE}\u@\h \W${BLUE}]${YELLOW}\$(parse_git_branch)${RED_BG}${WHITE}\$(parse_git_behind)${ANSI_OFF}$ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment