Skip to content

Instantly share code, notes, and snippets.

@postazure
Created July 4, 2018 09:48
Show Gist options
  • Save postazure/4071a7b4b749775716411e5f9516ec6d to your computer and use it in GitHub Desktop.
Save postazure/4071a7b4b749775716411e5f9516ec6d to your computer and use it in GitHub Desktop.
Simple Git Bash Prompt
BLUE="$(tput setaf 4)"
GREEN="$(tput setaf 2)"
ORANGE="$(tput setaf 3)"
RED="$(tput setaf 1)"
RESET="$(tput sgr0)"
git_prompt(){
if [ -d .git ]; then
IS_GIT_REPO=true
else
IS_GIT_REPO=false
fi;
PROMPT_GIT_STATUS=$(git_status_prompt);
PROMPT_GIT_BRANCH=$(git_branch_prompt);
}
git_status_prompt(){
if [ "$IS_GIT_REPO" = true ] && [ -n "$(git status --porcelain)" ]; then
echo "${ORANGE}± "
else
echo ""
fi;
}
git_branch_prompt(){
if [ "$IS_GIT_REPO" = true ] ; then
branch=$(git rev-parse --abbrev-ref HEAD)
if [ $branch = "master" ] ; then
echo ""
else
echo $(git rev-parse --abbrev-ref HEAD)
fi;
else
echo ""
fi;
}
path_prompt(){
PS1X=$(p="${PWD#${HOME}}"; [ "${PWD}" != "${p}" ] && printf "~";IFS=/; for q in ${p:1}; do printf /${q:0:1}; done; printf "${q:1}")
}
PROMPT_COMMAND='git_prompt; path_prompt;'
PS1='${GREEN}${PROMPT_GIT_BRANCH}${PROMPT_GIT_STATUS}${BLUE}${PS1X} $ ${RESET}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment