Skip to content

Instantly share code, notes, and snippets.

@taha
Created August 26, 2012 21:37
Show Gist options
  • Save taha/3483751 to your computer and use it in GitHub Desktop.
Save taha/3483751 to your computer and use it in GitHub Desktop.
Command prompt customisation to show git branch and status
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
CYAN="\[\033[0;36m\]"
NO_COLOUR="\[\033[0m\]"
GIT_PROMPT_PREFIX="[git:"
GIT_PROMPT_SUFFIX="]$NO_COLOUR "
GIT_PROMPT_DIRTY="$RED+"
GIT_PROMPT_CLEAN="$GREEN"
function git_current_branch()
{
echo "$(git branch 2>/dev/null | sed -n '/^\*/s/^\* //p')"
}
function git_parse_dirty()
{
if [[ -n $(git status -s 2> /dev/null) ]]; then
echo "$GIT_PROMPT_DIRTY"
else
echo "$GIT_PROMPT_CLEAN"
fi
}
function git_prompt_info()
{
if ! git rev-parse --git-dir > /dev/null 2>&1; then
return 0
fi
echo "$(git_parse_dirty)$GIT_PROMPT_PREFIX$(git_current_branch)$GIT_PROMPT_SUFFIX"
}
function prompt_line()
{
printf "%s: %s%s" "$CYAN\u" "$YELLOW\w $(git_prompt_info)$NO_COLOUR" "→ "
}
PROMPT_COMMAND='PS1="$(prompt_line)"'
@taha
Copy link
Author

taha commented Aug 26, 2012

Preview of the script above

Preview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment