Skip to content

Instantly share code, notes, and snippets.

Last active October 13, 2021 11:03
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
My git branch prompt, colorizes the branch based on status. Might need some work on the colors as yet. Apart from the definition, works in bash or ksh.
function _git_prompt() {
local _branch=`git branch --no-color 2> /dev/null | sed -ne 's/^* //p'`
[[ -z $_branch ]] && return
local _bgcolor
local _color
for s in `git status --porcelain | tr ' ' '.' | cut -c 1-2`; do
if [ -z "$_color" ]; then
case "$s" in
?D) _color='\e[35m' ;;
?M) _color='\e[36m' ;;
\?\?) _color='\e[33m' ;;
if [ -z "$_bgcolor" ]; then
case $s in
A?) _bgcolor='\e[43m' ;;
D?) _bgcolor='\e[41m' ;;
M?) _bgcolor='\e[44m' ;;
# Highligh it we haven't done a git pull today
local now=`date +%s`
local last_pull=0
local fetch_head=$( git rev-parse --show-toplevel )/.git/FETCH_HEAD
[ -e $fetch_head ] && last_pull=`stat -c %Y $fetch_head`
age=$(( $now - $last_pull ))
if [ $age -gt 86400 ]; then
echo -en "\[\e[0m\]{\[$_bgcolor$_color\]${_branch}\[\e[0m\]}"
prompt_cmd() {
PS1="\n[$(date +%H:%M)]-[\u@\h: \w] $(_git_prompt) \$ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment