Skip to content

Instantly share code, notes, and snippets.

@agners
Created February 2, 2013 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save agners/4699573 to your computer and use it in GitHub Desktop.
Save agners/4699573 to your computer and use it in GitHub Desktop.
Git bash prompt
# Bash PS1 for Git repositories showing branch and relative path inside
# the Repository
# Reset
RESET="\[\033[0m\]"
# Regular Colors
BLACK="\[\033[0;30m\]"
RED="\[\033[0;31m\]"
GREEN="\[\033[0;32m\]"
YELLOW="\[\033[0;33m\]"
BLUE="\[\033[0;34m\]"
PURPLE="\[\033[0;35m\]"
CYAN="\[\033[0;36m\]"
WHITE="\[\033[0;37m\]"
# PS1 Prompt variables
USER="\u"
HOST="\h"
TIME12H="\T"
TIME12A="\@"
PATHSHORT="\W"
PATHFULL="\w"
NEWLINE="\n"
JOBS="\j"
__git_relative_dir() {
local dirname
if [ -d .git ]; then
dirname=""
else
dirname=" $(git rev-parse --show-prefix)"
fi
echo "$dirname"
}
# This function generates the prompt, depending on Git's status...
function __git_prompt()
{
local pre_prompt="${USER}@${HOST}"
git branch &>/dev/null
if [ "$?" -eq "0" ]; then
local git_prompt="$(__git_ps1 ' (%s)')"
local post_prompt="$(__git_relative_dir) \$ "
git status | grep "nothing to commit" > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
# Clean repository, show it in green..
PS1="$pre_prompt${GREEN}$git_prompt${RESET}$post_prompt"
else
# Repository dirty, show in red...
PS1="$pre_prompt${RED}$git_prompt${RESET}$post_prompt"
fi
# @2 - Prompt when not in GIT repo
else
PS1="$pre_prompt ${PATHFULL}${RESET}\$ "; \
fi
}
PROMPT_COMMAND=__git_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment