Skip to content

Instantly share code, notes, and snippets.

@ljayz
Last active September 7, 2018 04:21
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 ljayz/6331c5c216709edcf4a5377fb51f8de2 to your computer and use it in GitHub Desktop.
Save ljayz/6331c5c216709edcf4a5377fb51f8de2 to your computer and use it in GitHub Desktop.
Show git branch in bash terminal
# Install https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh to ~.git-prompt.sh
# Add to code below to ~.profile
# store colors
MAGENTA="\[\033[0;35m\]"
YELLOW="\[\033[01;33m\]"
BLUE="\[\033[00;34m\]"
LIGHT_GRAY="\[\033[0;37m\]"
CYAN="\[\033[0;36m\]"
GREEN="\[\033[32m\]"
RED="\[\033[0;31m\]"
VIOLET='\[\033[01;35m\]'
function color_my_prompt {
local __user_and_host="$GREEN\u@\h"
local __cur_location="$GREEN\w" # capital 'W': current directory, small 'w': full file path
local __git_branch_color="$LIGHT_GRAY"
local __prompt_tail="$VIOLET$"
local __user_input_color="$LIGHT_GRAY"
local __git_branch='$(__git_ps1)';
# colour branch name depending on state
if [[ "$(__git_ps1)" =~ "*" ]]; then # if repository is dirty
__git_branch_color="$RED"
elif [[ "$(__git_ps1)" =~ "$" ]]; then # if there is something stashed
__git_branch_color="$YELLOW"
elif [[ "$(__git_ps1)" =~ "%" ]]; then # if there are only untracked files
__git_branch_color="$RED"
elif [[ "$(__git_ps1)" =~ "+" ]]; then # if there are staged files
__git_branch_color="$CYAN"
fi
# Build the PS1 (Prompt String)
# PS1="$__user_and_host $__cur_location$__git_branch_color$__git_branch $__prompt_tail$__user_input_color "
PS1="$__cur_location$__git_branch_color$__git_branch $__prompt_tail$__user_input_color "
}
# configure PROMPT_COMMAND which is executed each time before PS1
export PROMPT_COMMAND=color_my_prompt
# if .git-prompt.sh exists, set options and execute it
if [ -f ~/.git-prompt.sh ]; then
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWSTASHSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWUPSTREAM="auto"
GIT_PS1_HIDE_IF_PWD_IGNORED=true
GIT_PS1_SHOWCOLORHINTS=true
. ~/.git-prompt.sh
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment