Created
October 19, 2011 19:45
-
-
Save presstube/1299452 to your computer and use it in GitHub Desktop.
Show handy git info in your terminal prompt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this snippet to your .bash_profile to show handy git info in your prompt | |
Thanks to http://opinionated-programmer.com/2011/01/colorful-bash-prompt-reflecting-git-status/ | |
# Snazzy git prompt | |
function _git_prompt() { | |
local git_status="`git status -unormal 2>&1`" | |
if ! [[ "$git_status" =~ Not\ a\ git\ repo ]]; then | |
if [[ "$git_status" =~ nothing\ to\ commit ]]; then | |
local ansi=42 | |
#local ansi=32 | |
elif [[ "$git_status" =~ nothing\ added\ to\ commit\ but\ untracked\ files\ present ]]; then | |
local ansi=43 | |
else | |
#local ansi=45 | |
local ansi=41 | |
fi | |
if [[ "$git_status" =~ On\ branch\ ([^[:space:]]+) ]]; then | |
branch=${BASH_REMATCH[1]} | |
test "$branch" != master || branch=' ' | |
else | |
# Detached HEAD. (branch=HEAD is a faster alternative.) | |
branch="(`git describe --all --contains --abbrev=4 HEAD 2> /dev/null || | |
echo HEAD`)" | |
fi | |
echo -n '\[\e[0;37;'"$ansi"';1m\]'"$branch"'\[\e[0m\] ' | |
fi | |
} | |
function _prompt_command() { | |
#PS1="`_git_prompt`"'... your usual prompt goes here, e.g. \[\e[1;34m\]\w \$\[\e[0m\] ' | |
PS1="`_git_prompt`"'\W >> ' | |
} | |
PROMPT_COMMAND=_prompt_command | |
# end snazzy git prompt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment