My bash prompt with git/svn branch+status display

After spending a few hours last night switching between three different branches in the ack2 project, and typing “git br” over and over, I decided I needed to put branch status in my bash prompt. The only question was: Which one would I steal? Fortunately, Rob Hoelz was online and I mentioned it to him and he handed me his, so I stole it and also added Subversion support as well.

Note: Edited to fix a problem with detecting SVN branches.


# Adapted from from

function __prompt
    # List of color variables that bash can use
    local BLACK="[33[0;30m]"   # Black
    local DGREY="[33[1;30m]"   # Dark Gray
    local RED="[33[0;31m]"     # Red
    local LRED="[33[1;31m]"    # Light Red
    local GREEN="[33[0;32m]"   # Green
    local LGREEN="[33[1;32m]"  # Light Green
    local BROWN="[33[0;33m]"   # Brown
    local YELLOW="[33[1;33m]"  # Yellow
    local BLUE="[33[0;34m]"    # Blue
    local LBLUE="[33[1;34m]"   # Light Blue
    local PURPLE="[33[0;35m]"  # Purple
    local LPURPLE="[33[1;35m]" # Light Purple
    local CYAN="[33[0;36m]"    # Cyan
    local LCYAN="[33[1;36m]"   # Light Cyan
    local LGREY="[33[0;37m]"   # Light Gray
    local WHITE="[33[1;37m]"   # White

    local RESET="[33[0m]"      # Color reset
    local BOLD="[33[;1m]"      # Bold

    # Base prompt

    local dirty
    local branch

    # Look for Git status
    if git status &>/dev/null; then
        if git status -uno -s | grep -q . ; then
        branch=$(git branch --color=never | sed -ne 's/* //p')

    # Look for Subversion status
        svn_info=$( (svn info | grep ^URL) 2>/dev/null )
        if [[ ! -z "$svn_info" ]] ; then
            branch_pattern="^URL: .*/(branch(es)?|tags)/([^/]+)"
            trunk_pattern="^URL: .*/trunk(/.*)?$"
            if [[ $svn_info =~ $branch_pattern ]]; then
            elif [[ $svn_info =~ $trunk_pattern ]]; then
            dirty=$(svn status -q)

    if [[ ! -z "$branch" ]]; then
        local status_color
        if [[ -z "$dirty" ]] ; then
        PS1="$LCYAN($BOLD$status_color$branch$LCYAN)$RESET $PS1"

if [[ -z "$PROMPT_COMMAND" ]]; then

Just drop that into your ~/.bash directory as, and then add

source ~/.bash/

to your .bashrc. Now you have color-coded branch names: red for dirty, green for clean.


Tags: , , , ,

2 Responses to “My bash prompt with git/svn branch+status display”

  1. Greg Says:

    Great script! I tweaked it a bit to show bzr status as well. Virtually the same commands as svn, except for a couple of items.

    The dirty flag needs to be set like this:
    dirty=$(bzr status -SV)

    And the patterns need to be matched dependent on your repo scheme. For ours, this is what we use:

    trunk_pattern=”^ checkout of branch: .*/trunk/$”
    branch_pattern=”^ checkout of branch: .*/feature/([^/]+)”

    And we also have a release branch, so I added this one:
    release_pattern=”^ checkout of branch: .*/release/([^/]+)”

    And you have to set the branch like this instead:

    Finally, there’s a small typo in the Bold color; it should be this:
    local BOLD=”[33[1m]”
    instead of
    local BOLD=”[33[;1m]”

    Thanks for ack! and the other great content you post!

  2. Mohammad S Anwar Says:

    Cool tips. However there is typo in it, If I am not mistaken.

    The line:
    branch=$(git branch –color=never | sed -ne ‘s/* //p’)

    should be like:

    branch=$(git branch –no-color | sed -ne ‘s/* //p’)

    Best Regards,
    Mohammad S Anwar

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: