Last active
June 27, 2018 16:57
-
-
Save jost125/7337206 to your computer and use it in GitHub Desktop.
basic git 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 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="${GREEN}${USER}${RESET}${CYAN}@${HOST}${RESET}" | |
git branch &>/dev/null | |
if [ "$?" -eq "0" ]; then | |
local git_prompt="$(__git_ps1 ' (%s)')" | |
local post_prompt="$(__git_relative_dir) \$ " | |
PS1="$pre_prompt${GREEN}$git_prompt${RESET}${PATHFULL}\$ " | |
# @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