Last active
October 19, 2015 08:15
-
-
Save rbarbey/120e4d254daeb3884e95 to your computer and use it in GitHub Desktop.
Git bash prompt
This file contains 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
function git_prompt { | |
# get the current status in short notation | |
local status=$(git status -sb 2> /dev/null) | |
# if we don't get any output, we're not in a git repo | |
if [ -z "$status" ]; then | |
return | |
fi | |
# determine color for displaying the branch name | |
# default=no changes; yellow=merge; red=untracked/modified; green=added | |
local color="\e[39m" | |
if [ ! -z "$(egrep '^UU' <<< "$status")" ];then | |
color="\e[33m" | |
elif [ ! -z "$(egrep '^[^#][^ ]' <<< "$status")" ]; then | |
color="\e[31m" | |
elif [ ! -z "$(egrep '^[MARCD]' <<< "$status")" ]; then | |
color="\e[32m" | |
fi | |
# get name of the branch and ahead/behind info | |
local status_line=$(head -1 <<< "$status") | |
local branch=$(sed -e "s/^## \([^\.]*\).*/\1/" <<< "$status_line") | |
local ahead=$(sed -e "/.*ahead \([0-9][0-9]*\).*$/!d;s//\1/" <<< "$status_line") | |
if [ -z "$ahead" ]; then | |
ahead="0" | |
fi | |
local behind=$(sed -e "/.*behind \(.*\)]/!d;s//\1/" <<< "$status_line") | |
if [ -z "$behind" ]; then | |
behind="0" | |
fi | |
# e.g. [develop 1↑5↓] | |
echo -e " [$color$branch\e[0m $ahead\u2191$behind\u2193] " | |
} | |
export PS1='\u@\h:\w$(git_prompt)$ ' |
Thanks for that. My script above was written on a Mac with macports and bash 4.3 installed. I think the \e
directives are only working on bash 4.x.
Now deprecated. Use magicmonty/bash-git-prompt if you can.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool stuff. On Mac I am using the following script. It does not include the ahead/behind state, but it should work for Mac in case Roberts script does not: