Skip to content

Instantly share code, notes, and snippets.

@dplord
Created October 15, 2015 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dplord/d56693fa965b64a31e4b to your computer and use it in GitHub Desktop.
Save dplord/d56693fa965b64a31e4b to your computer and use it in GitHub Desktop.
function find_git_branch {
local dir=. head
until [ "$dir" -ef / ]; do
if [ -f "$dir/.git/HEAD" ]; then
head=$(< "$dir/.git/HEAD")
if [[ $head == ref:\ refs/heads/* ]]; then
git_branch=" ${head#*/*/}"
elif [[ $head != '' ]]; then
git_branch=' (detached)'
else
git_branch=' (unknown)'
fi
return
fi
dir="../$dir"
done
git_branch=''
}
PROMPT_COMMAND="find_git_branch; $PROMPT_COMMAND"
green=$'\e[0;32m'
magenta=$'\e[0;35m'
normal_colours=$'\e[m'
PS1="\[$green\]\u@\h:\w\[$magenta\]\$git_branch\[$green\]\\$\[$normal_colours\] "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment