Rusty Russell's Coding Blog | Stealing From Smart People

Jul/13

27

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

RSS Feed

4 Comments for Git prompt for bash

Amit Shah | July 28, 2013 at 10:47 pm

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)”)\$ ‘

Author comment by rusty | July 29, 2013 at 9:43 am

Hmm, latest Ubuntu and I don’t have that in contrib/. (git 1.8.1.2).

But it’s spectacular…

Someone also points out vcprompt: http://hg.gerg.ca/vcprompt

Cheers,
Rusty.

Mah | July 31, 2013 at 6:34 pm

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).

mikey | August 11, 2013 at 7:03 pm

Freaking awesome! Where have you been all my life!?!?

Leave a comment!

«

»

Find it!

Theme Design by devolux.org

Tag Cloud