Skip to content

Instantly share code, notes, and snippets.

@esod
Last active October 15, 2020 02:26
Show Gist options
  • Save esod/ca092876637ebe21d0466a0e4b76c0fe to your computer and use it in GitHub Desktop.
Save esod/ca092876637ebe21d0466a0e4b76c0fe to your computer and use it in GitHub Desktop.
Display path, git branch, and status in terminal
# Display path, git branch, and status in terminal
################################################
# Git prompt
# Many thanks to Andy Magoon https://gist.github.com/magoon/6002421
#
# After (branch name), you'll see one or more of
# • uncommitted changes in green
# • unstaged changes in yellow
# • untracked files in blue(ish)
# 1 number of stashes for this branch, also in blue(ish)
#
# To match the bullet colors with git status colors, add
#
# [color "status"]
# added = green
# changed = yellow
# untracked = blue
#
# to your ~/.gitconfig.
################################################
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
BRIGHTGREEN="\[\033[1;32m\]"
BLUE="\[\033[1;34m\]"
CYAN="\[\033[1;36m\]"
MAGENTA="\[\033[0;35m\]"
NO_COLOUR="\[\033[0m\]"
PROMPT_CHAR='$ '
# Change git color if branch is dirty
function parse_git_staged() {
local git_status="`git status -unormal 2>&1`"
if [[ "$git_status" =~ Changes\ to\ be\ committed ]] ; then
echo -n "•"
fi
}
function parse_git_unstaged() {
local git_status="`git status -unormal 2>&1`"
if [[ "$git_status" =~ Changes\ not\ staged\ for\ commit ]] ; then
echo -n "•"
fi
}
function parse_git_untracked() {
local git_status="`git status -unormal 2>&1`"
if [[ "$git_status" =~ Untracked\ files ]] ; then
echo -n "•"
fi
}
# Show the branch name in parens
function parse_git_branch () {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
# Show count of stashes for this branch
function parse_git_stashes () {
git stash list 2> /dev/null | grep "n `git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`:" | wc -l | sed 's/^ *//' | sed 's/ *$//' | sed 's/^0$//'
}
PS1="$CYAN\$(date +%H:%M) $BLUE \w$RED \$(parse_git_branch)$YELLOW\$(parse_git_staged)$BRIGHTGREEN\$(parse_git_unstaged)$BLUE\$(parse_git_untracked)$BLUE\$(parse_git_stashes)$NO_COLOUR${PROMPT_CHAR}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment