Skip to content

Instantly share code, notes, and snippets.

@20after4
Created January 10, 2012 06:35
Show Gist options
  • Save 20after4/1587443 to your computer and use it in GitHub Desktop.
Save 20after4/1587443 to your computer and use it in GitHub Desktop.
Bash prompt that indicates current git branch
# This depends on git bash completion:
# https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
BLACK="\[$(tput setaf 0)\\"
RED="\[$(tput setaf 1)\]"
GREEN="\[$(tput setaf 2)\]"
LIME="\[$(tput setaf 190)\]"
YELLOW="\[$(tput setaf 3)\]"
POWDER_BLUE="\[$(tput setaf 153)\]"
BLUE="\[$(tput setaf 4)\]"
MAGENTA="\[$(tput setaf 5)\]"
CYAN="\{$(tput setaf 6)\]"
WHITE="\[$(tput setaf 7)\]"
BRIGHT="\[$(tput bold)\]"
NORMAL="\[$(tput sgr0)\]"
UNDERLINE="\[$(tput smul)\]"
export GIT_PS1_SHOWUPSTREAM="auto"
function parse_git_branch {
__git_ps1 "(%s)"
# this is really not necessary since __git_ps1 does a much better job than my own crude shell scripting
# git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
# default prompt
PS1_DEFAULT="${YELLOW}me${NORMAL}@${RED}\h${NORMAL}:${BLUE}\w${NORMAL}"
function ps1 {
# this swaps between a git prompt and default (above) based on PWD
# set it to match where you keep your git repositories or just leave it always enabled
# __git_ps1 handles it gracefully but I wanted to differentiate between git and non-git locations.
if [[ "${PWD}" == /src/*/* ]]; then
PS1="$PS1_DEFAULT ${RED}\$(parse_git_branch)${NORMAL}"
else
PS1="$PS1_DEFAULT"
fi
PS1="$PS1 ${YELLOW}↪${NORMAL} "
}
PROMPT_COMMAND="ps1; $PROMPT_COMMAND"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment