-# The git functions are from a long-forgotten gist or blog or something.
-# Credit to unknown upstream author for those.
-# This snippet is just so I can share it.
+# Usage: source this file in your .bash_profile or .bashrc
-if [[ "$environment" == *CYGWIN* ]]
+# Effect: sets $PS1 to show git branch, dirty and stash status.
+# Stash skipped in cygwin as it was too slow when tested, YMMV.
+# To insert the git smarts into your own PS1, add this:
+# $(git branch &>/dev/null;if [ $? -eq 0 ]; then echo " $(parse_git_branch)$(parse_git_dirty)$(parse_git_stash)"; fi)
- function parse_git_branch {
- git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1/"
- export PS1='\e[1;30m\][\e[\e[1;30m\]\e[1;33m\] \u@\H \[\e[1;32m\]\w\[\e[0m\] $(parse_git_branch) (check status manually) \e[1;30m\]]\e[37m\]\n> '
- echo "Cygwin prompt formatting loaded - note no git status or stash parsing (too damn slow)"
+ CYGWIN*) OS="cygwin" ;;
+ MINGW32*) OS="windows" ;;
+ *) echo "OS not detected successfully" ;;
+# check for WSL (bash-on-ubuntu-on-windows)
+# can be removed if you don't need to detect WSL
+if [[ $OS = "linux" ]]; then
+ if $(grep --silent 'Microsoft' /proc/sys/kernel/osrelease); then
-if [[ "$environment" == *Darwin* ]]
- # mac version - git stash handled a little differently
- function parse_git_dirty {
- [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working directory clean" ]] && echo "(*)"
- function parse_git_stash {
- [[ $(git stash list 2> /dev/null | tail -n1) != "" ]] && echo "^"
- function parse_git_branch {
- git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1$(parse_git_dirty)/"
- # Minimal version with standard prompt and git output:
- # export PS1='\u@\H \w $(git branch &>/dev/null;if [ $? -eq 0 ]; then echo "$(parse_git_branch)$(parse_git_stash)"; fi) $ '
+if [[ $OS == 'osx' ]];then
+ GIT_DIRTY_MESSAGE="nothing to commit, working tree clean"
+ GIT_DIRTY_MESSAGE="nothing to commit, working directory clean"
- export PS1='\e[1;37m\][\e[\e[1;37m\]\e[1;33m\] \u@\H \[\e[1;32m\]\w\[\e[0m\]$(git branch &>/dev/null;if [ $? -eq 0 ];
- then echo " \[\033[01;34m\]\e[1;37m$(parse_git_branch)$(parse_git_stash)"; fi) \e[1;37m\]] \e[37m\]\n\[$PROMPT\]$ '
- echo "Prompt formatting loaded"
+ [[ $(git stash list 2> /dev/null | tail -n1) != "" ]] && echo "^"
+ git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1/"
+ [[ $(git status 2> /dev/null | tail -n1) != $GIT_DIRTY_MESSAGE ]] && echo "(*)"
+if [[ $OS == 'cygwin' ]]
+ export PS1='\e[1;30m\][\e[\e[1;30m\]\e[1;33m\] \u@\H \[\e[1;32m\]\w\[\e[0m\]$(git branch &>/dev/null;if [ $? -eq 0 ]; then echo " \[\033[01;34m\]\e[1;37m$(parse_git_branch)$(parse_git_dirty)"; fi) \e[1;30m\]] \n$\e[37m\] '
+ echo "Cygwin prompt formatting loaded - note no git stash parsing"
+ export PS1='\[\033[0;37m\][ \[\033[1;33m\]\u@\H \[\033[0;32m\]\w\[\033[0;37m\]$(git branch &>/dev/null;if [ $? -eq 0 ]; then echo " $(parse_git_branch)$(parse_git_dirty)$(parse_git_stash)"; fi) ]\n\$ \[\033[0m\]'