Adding lots of git love to your prompt

There are two things that are really useful to see *at a glance* while writing code:

  1. What branch am I on?
  2. Have I modified stuff?

NINJA EDIT: Alan Antonuk has mentioned that there already are such bash prompts in git. Here’s what I’m now using:

#git ps_1
if [ -f ~/.git-prompt.sh ]; then
    export GIT_PS1_SHOWDIRTYSTATE="true"
    export GIT_PS1_SHOWSTASHSTATE="true"
    export GIT_PS1_SHOWUNTRACKEDFILES="true"
    . ~/.git-prompt.sh
    export PS1="\$(__git_ps1)$"
fi

You can get the latest bash_prompt.sh from https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh.

And here’s what I used to use:
I added some stuff to PS1 so my bash prompt shows both the the branch name and the diff status.

Define two functions in your ~/.bash_profile:

    function parse_git_branch {
        git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \[\1\]/'
    }
    function check_modified_git {
        if git diff --quiet 2> /dev/null && git diff --cached --quiet 2> /dev/null
        then
            echo "*"
        else
            echo ""
        fi
    }

and wrap the change to PS1 with a test for git – just in case your bash_profile is executed on a machine without git installed:

if hash git 2>/dev/null; then
    export PS1="\$(parse_git_branch)\$(check_modified_git)$"
fi

Of course, you can do fancy things with colors. There’s lots of explanations of the bash color codes online; here’s one: http://www.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html