-
-
Save mark-d-holmberg/b3f3f57ae845b9890a0f441c3d8937e0 to your computer and use it in GitHub Desktop.
Git Bash 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
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="[${G}\${REPO_NAME} ${R}(L) \${BRANCH_NAME} --> ${Y}(R) \${TRACK_BRANCH_NAME}${NONE}] ${OLD_PS1}" | |
else | |
: # this is not a git repository | |
BRANCH_NAME="" | |
PS1=$OLD_PS1 | |
fi | |
} | |
bash_prompt() { | |
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