Skip to content

Instantly share code, notes, and snippets.

@herczy
Created March 18, 2011 14:27
Show Gist options
  • Select an option

  • Save herczy/876156 to your computer and use it in GitHub Desktop.

Select an option

Save herczy/876156 to your computer and use it in GitHub Desktop.
Show git status in bash prompt
git_branch()
{
# Check if we are on an untrached branch
ISUNTRACKED=$(git branch 2>/dev/null | grep '* (no branch)' | wc -l)
# Checks the branch name
BRANCH=$(git branch 2>/dev/null | grep '*' | awk '{ print $2 }')
if [ "x$BRANCH" != "x" ]; then
if [ "x$ISUNTRACKED" != "x0" ]; then
echo -ne ':\033[31;1mUNTRACKED('$(git log --oneline -1 | awk '{ print $1 }')')\033[0m'
else
echo -ne ':\033[36;1m'$BRANCH'\033[00m'
fi
# Checks the relative directory to the
# git repository
DIR=`pwd`
while [ "x$DIR" != "x/" ]; do
if [ -d "$DIR/.git" ]; then
CWD=`pwd`
DIR="${CWD/$DIR/}"
if [ "x$DIR" != "x" ]; then
echo -ne '\033[30;1m('$DIR')\033[0m'
fi
break
fi
DIR=`dirname $DIR`
done
TAGS=
if [ "x$ISUNTRACKED" != "x" ]; then
HASORIG=$(git branch -r | grep origin/$BRANCH | wc -l)
if [ "x$HASORIG" != 'x0' ]; then
DIFF=$(git log --oneline origin/$BRANCH..HEAD | wc -l)
if [ "x$DIFF" != "x0" ]; then
TAGS="$TAGS unpushed:$DIFF"
fi
fi
fi
# Check if repo is dirty
DIFF=$(git status --porcelain | wc -l)
if [ "x$DIFF" != "x0" ]; then
TAGS="$TAGS modified:$(git status --porcelain | grep ' M' | wc -l)"
TAGS="$TAGS untracked:$(git status --porcelain | grep '??' | wc -l)"
TAGS="$TAGS sum:$DIFF"
fi
if [ "x$TAGS" != "x" ]; then
TAGS=$(echo $TAGS | sed 's/^ //')
echo -ne ' [\033[35;1m'$TAGS'\033[0m]'
fi
fi
}
PS1='\n${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(git_branch)\n\$ '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment