Skip to content

Instantly share code, notes, and snippets.

@yonik
Created January 23, 2016 18:19
Show Gist options
  • Save yonik/04b2e759eecf5ef96383 to your computer and use it in GitHub Desktop.
Save yonik/04b2e759eecf5ef96383 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"
RED="\\[\\033[31m\\]"
GREEN="\\[\\033[32m\\]"
YELLOW="\\[\\033[33m\\]"
BLUE="\\[\\033[34m\\]"
MAGENTA="\\[\\033[35m\\]"
WHITE="\\[\\033[0m\\]"
export PS1="$MAGENTA\$git_branch$GREEN\w$ $WHITE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment