Git prompt for bash

I don’t know who wrote this originally, but this is from my .bashrc.  Tridge’s is simpler, but has colour!

Before this, I avoided git branches in favour of multiple copies of repositories because I use my prompt to provide location.  This provided the missing piece…

# Git me harder!
__git_ps1 ()
{
    local g="$(git rev-parse --git-dir 2>/dev/null)"
    if [ -n "$g" ]; then
        local r
        local b
        if [ -d "$g/../.dotest" ]
        then
            local b="$(git symbolic-ref HEAD 2>/dev/null)"
            r="|REBASING"
        elif [ -d "$g/.dotest-merge" ]
        then
            r="|REBASING"
            b="$(cat $g/.dotest-merge/head-name)"
        elif [ -f "$g/MERGE_HEAD" ]
        then
            r="|MERGING"
            b="$(git symbolic-ref HEAD 2>/dev/null)"
        else
            if [ -f $g/BISECT_LOG ]
            then
                r="|BISECTING"
            fi
            if ! b="$(git symbolic-ref HEAD 2>/dev/null)"
            then
                b="$(cut -c1-7 $g/HEAD)..."
            fi
        fi
        if [ -n "$1" ]; then
            printf "$1" "${b##refs/heads/}$r"
        else
            printf " (%s)" "${b##refs/heads/}$r"
        fi
    fi
}

PS1="${PS1//\\w/\\w\$(__git_ps1)}"

4 thoughts on “Git prompt for bash”

  1. I have this:

    source /usr/share/git-core/contrib/completion/git-prompt.sh
    export PS1=’\[33[01;34m\][\t]\[33[00m\] \[33[00;36m\]\u@\h\[33[00m\]:\[33[01;34m\] \w\[33[00m\]$(__git_ps1 ” (%s)”)\$ ‘

  2. It should already be in /etc/bash_completion.d/git-prompt

    Just add __git_ps1 somewhere in your PS1

    You can also export “GIT_PS1_SHOWDIRTYSTATE=1” and other stuff for more details
    (see /usr/lib/git-core/git-sh-prompt).

Comments are closed.