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 ~/ ]; then
    export GIT_PS1_SHOWDIRTYSTATE="true"
    export GIT_PS1_SHOWSTASHSTATE="true"
    . ~/
    export PS1="\$(__git_ps1)$"

You can get the latest from

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
            echo "*"
            echo ""

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)$"

Of course, you can do fancy things with colors. There’s lots of explanations of the bash color codes online; here’s one:

Leave a Reply

Your email address will not be published. Required fields are marked *