Skip to content

Instantly share code, notes, and snippets.

@wence-
Created January 24, 2014 17:52
Show Gist options
  • Select an option

  • Save wence-/8602440 to your computer and use it in GitHub Desktop.

Select an option

Save wence-/8602440 to your computer and use it in GitHub Desktop.
git_branch=''
function find_git_branch {
local dir=. head staged dirty stash
until [ "$dir" -ef / ]; do
if [ -f "$dir/.git/HEAD" ]; then
git_branch=" ${red}g${green}"
head=$(< "$dir/.git/HEAD")
dirty=''
git --git-dir="${dir}git" --work-tree="${dir}" diff --no-ext-diff \
--quiet --exit-code || \
dirty="yes"
staged=''
if git rev-parse --quiet --verify HEAD > /dev/null; then
git diff-index --cached --quiet HEAD -- || \
staged="yes"
fi
stash=''
# stash is just a file in refs/stash
if [[ -f "$dir/.git/refs/stash" ]]; then
stash="yes"
fi
if [[ $head == ref:\ refs/heads/* ]]; then
git_branch="$git_branch ${head#*/*/}"
elif [[ $head != '' ]]; then
git_branch="$git_branch (detached: ${head:0:6})"
else
git_branch='$git_branch (unknown)'
fi
if [[ $staged != '' ]]; then
git_branch="$git_branch ${yellow}s"
fi
if [[ $dirty != '' ]]; then
git_branch="$git_branch ${red}d"
fi
if [[ $stash != '' ]]; then
git_branch="$git_branch ${magenta}st"
fi
return
fi
dir="../$dir"
done
git_branch=''
}
blue=$'\e[1;34m'
magenta=$'\e[1;35m'
red=$'\e[1;31m'
normal=$'\e[m'
green=$'\e[1;32m'
yellow=$'\e[1;33m'
export PROMPT_COMMAND="find_git_branch;"
export PS1='\u@\h:\[$blue\]\w\[$green\]$git_branch\[$normal\]\n\$ '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment