PS1 prompt for GIT
It is easy to shoot your foot off with git, but also easy to revert to a previous foot and merge it with your current leg.
— Jack William Bell
The first step to becoming more proficient with GIT is being aware about the current status of your repository. Of course this is where your trusty companion git status is indispensable. However, if you are a developer or if you work with GIT on a daily basis. It is very useful to be aware of the GIT status at all times.
The easiest way to do this is by modifying PS1 in your terminal.
What it looks like:
Clean state: 
Untracked changes: 
Added changes: 
These are only a couple of examples. The example PS1 I will show here also tracks the difference between the remote and local branch. But lets get to the nugget.
Modify your .bashrc
__rc_parse_git_branch() {
local branch_short=$(git branch 2> /dev/null | sed -e '/^[^*]/d' | sed 's/^[*] //g' | cut -c -30)
echo "$branch_short"
}
__rc_git_status() {
# Outputs a series of indicators based on the status of the
# working directory:
# + changes are staged and ready to commit
# X unstaged changes are present
# ? untracked files are present
# (up arrow) local branch ahead of remote
# (down arrow) local branch behind remote
# S changes have been stashed
local status="$(git status --branch --porcelain 2>/dev/null)"
local output=''
[[ -n $(egrep '^[MADRC]' <<<"$status") ]] && output="${output}+"
[[ -n $(egrep '^.[MD]' <<<"$status") ]] && output="${output}X"
[[ -n $(egrep '^\?\?' <<<"$status") ]] && output="${output}?"
[[ -n $(fgrep 'behind' <<<"$status") ]] && output="${output}↓ "
[[ -n $(fgrep 'ahead' <<<"$status") ]] && output="${output}↑ "
[[ -n $(git stash list) ]] && output="${output}S"
[[ -z $output ]] && output='✓ '
output="[$output]"
echo $output
}
__rc_git_prompt() {
# First, get the branch name...
local branch=$(__rc_parse_git_branch)
# Empty output? Then we're not in a Git repository, so bypass the rest
# of the function, producing no output
if [[ -n $branch ]]; then
local state=$(__rc_git_status)
# Now output the actual code to insert the branch and status
echo "$branch $state "
fi
}
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \[\033[01;93m\]$(__rc_git_prompt)\[\033[00m\]\$ '
Note: In case the unicode characters give you issues regarding line wrapping, you can enter the control characters \001 and \002 which are versions of \[ and \] which work when echoed out by functions. These allow you to correct the character count. For example, bash might consider the checkmark two characters wide, but it should be considered as one. Placing a character between these control codes will make it not count towards the character count.
