Last Updated: February 25, 2016
·
1.079K
· mcloide

Path formatting and Git Branch on bash_profile

This helps a ton. It will show the path that you are in, current time and the git branch (if in a git repo) that you are working on.

The resulting on the bash_prompt should look like: [1:0:502][$USER@hostname:][~] [git_branch]

if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
c_reset=`tput sgr0`
c_user=`tput setaf 2; tput bold`
c_path=`tput setaf 4; tput bold`
c_git_clean=`tput setaf 2`
c_git_dirty=`tput setaf 1`
else
c_reset=
c_user=
c_path=
c_git_clean=
c_git_dirty=
fi

git_prompt () {
if ! git rev-parse --git-dir > /dev/null 2>&1; then
    return 0
fi

git_branch=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')

if git diff --quiet 2>/dev/null >&2; then
    git_color="${c_git_clean}"
else
    git_color=${c_git_dirty}
fi

echo " [$git_color$git_branch${c_reset}]"
}

export PS1="\[\033[0;32m\]\u@\h\[\033[0;39m\]:\[\033[0;35m\]\w\[\033[0;39m\]\$"
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Shell prompt format => [1:0:502][$USER@hostname:][~] [git_branch]
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
AAPS[0]="\n\e[1;30m[\e[0;37m${SHLVL}\e[1;30m:\e[0;37m\j:\!\e[1;30m][\e[1;34m\u\e[0;34m@\e[1;34m\h\e[1;30m:\e[1;37m${SSHTTY/\/dev\/}\e[1;30m]\e[0;37m[\e[0;37m\w\e[0;37m]\$(git_prompt)\e[1;37m\n\[${R}\]\$ ";
AAPS[1]='\n\e[1;30m[\e[0;37m${SHLVL}\e[1;30m:\e[0;37m\j:\!\e[1;30m][\e[0;32m\u\e[1;32m@\e[0;32m\h\e[1;30m:\e[1;37m${SSHTTY/\/dev\/}\e[1;30m]\e[0;37m[\e[0;37m\w\e[0;37m]\e[1;37m\n\[${R}\]\$ ';
AAPS[2]='\n\e[1;30m[\e[0;37m${SHLVL}\e[1;30m:\e[0;37m\j:\!\e[1;30m][\e[0;35m\u\e[1;35m@\e[0;35m\h\e[1;30m:\e[1;37m${SSHTTY/\/dev\/}\e[1;30m]\e[0;37m[\e[0;37m\w\e[0;37m]\e[1;37m\n\[${R}\]\$ ';
: ${PLVL=0};
[[ "${#AAPS[@]}" -lt "$PLVL" || "${#AAPS[@]}" -eq "$PLVL" ]] && PLVL=0;
export PS1=${AAPS[$PLVL]} && (( PLVL++ )) && export PLVL;

1 Response
Add your response

Or you could use the one that comes packaged with git that will handle conflicts, merges, etc:

/etc/bash_completion.d/git

_gitps1

over 1 year ago ·