Skip to content

Instantly share code, notes, and snippets.

@deevis
Last active February 13, 2018 13:59
Show Gist options
  • Save deevis/4c025f365cc44589a603 to your computer and use it in GitHub Desktop.
Save deevis/4c025f365cc44589a603 to your computer and use it in GitHub Desktop.
Git Bash Prompt
bash_prompt_command() {
local K="\[\033[0;30m\]" # black
local R="\[\033[0;31m\]" # red
local G="\[\033[0;32m\]" # green
local Y="\[\033[0;33m\]" # yellow
local B="\[\033[0;34m\]" # blue
local M="\[\033[0;35m\]" # magenta
local C="\[\033[0;36m\]" # cyan
local W="\[\033[0;37m\]" # white
local NONE="\[\033[0m\]"
# BRANCH_NAME=`git branch | grep "*" | awk '{FS=" "; print $2}' 2>&1 >/dev/null`
if git rev-parse --git-dir > /dev/null 2>&1; then
: # This is a valid git repository (but the current working
# directory may not be the top level.
# Check the output of the git rev-parse command if you care)
# BRANCH_NAME=`git branch | grep "*" | awk '{FS=" "; print $2}'`
# Get branch name
BRANCH_NAME=`git branch -vv| grep "*" | awk '{FS=" "; print $2}'`
# Get tracking branch name
TRACK_BRANCH_NAME=`git branch -vv| grep "*" | awk '{FS=" "; print $4}'`
# If the tracking_branch_name starts with [, it means we have one
if [[ $TRACK_BRANCH_NAME == \[* ]]; then
# Removes the first character
TRACK_BRANCH_NAME=${TRACK_BRANCH_NAME:1}
# Get branch name after last / "origin/1.2" -> "1.2"
TRACK_BRANCH_NAME=${TRACK_BRANCH_NAME##*/}
# Removes the last character either ] or :
TRACK_BRANCH_NAME=${TRACK_BRANCH_NAME::-1}
else
# We don't have a tracking branch
TRACK_BRANCH_NAME="---"
fi
REPO_NAME=`git remote -v | grep origin | grep fetch | sed 's/.*\/\(.*\).git .*/\1/g'`
# PS1="\[$(ppwd)\]\u@\h:\w \[\033[0;31m\][\${BRANCH_NAME}--\${TRACK_BRANCH_NAME}]\[\033[0m\] >"
PS1="[${G}\${REPO_NAME} ${R}\${BRANCH_NAME}--${Y}\${TRACK_BRANCH_NAME}${NONE}] ${OLD_PS1}"
else
: # this is not a git repository
BRANCH_NAME=""
# PS1="\[$(ppwd)\]\u@\h:\w >"
PS1=$OLD_PS1
fi
}
bash_prompt() {
#PS1="\[$(ppwd)\]\u@\h:\w \[\033[0;31m\][\${BRANCH_NAME}]\[\033[0m\] >"
OLD_PS1=$PS1
}
# init it by setting PROMPT_COMMAND
PROMPT_COMMAND=bash_prompt_command
bash_prompt
unset bash_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment