Pimp your command-line for git

I do development on both the Mac and windows. I prefer to use git as my source control these days and have done so for the past year or so.  Git is great, I love it. I love the ease of branching a lot too. I’ll often just branch of locally just to play around with an idea without affecting the master branch.

But having many branches can be confusing at times, especially in my case as I can only remember what I was doing for 5 seconds. So sometimes I mess up a perfectly good branch because of the confusion.

On windows I use Powershell as my command-line. I don’t know much of powershell scripting. To be honest I mainly started using it because then I wouldn’t constantly get an error when I typed ls instead of dir :)   However since then I did explore that environment a little and it _does_ give me easy access to the CLR and a way to create very very powerful batch files, although I do most of my scripting in Ruby these days.  It also allows you to customize your prompt. You need to allow scripts unrestricted access for this to work. You can do that by entering Set-ExecutionPolicy Unrestricted at a powershell prompt. Then you close powershell and create a file in %MYDOCUMENTS%\WindowsPowershell called profile.ps1

function prompt
{
    $host.ui.rawui.WindowTitle = $(get-location)
    Write-Host ("+ " + $(get-location)) -foregroundcolor Yellow
    
        $branches = ""
        git branch | foreach {
            if($_ -match "^\*\s(.*)"){
                $branches += $matches[1]
            }
        }
    if($branches){
        Write-Host ("(" + $branches + ") ") -nonewline -fore Cyan
    }
        
    
    Write-Host ("»") -nonewline -foregroundcolor Green
    return " "
}

The result of this prompt looks like this:

Picture 2

In bash I use a .bashrc script that shows me the branch in my prompt. You need ttycolors enabled to enjoy the full prompt but this is the section that takes care of my prompt.

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
    # We have color support; assume it's compliant with Ecma-48
    # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
    # a case would tend to support setf rather than setaf.)
    color_prompt=yes
    else
    color_prompt=
    fi
fi
 
parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
 
 
# export PS1='\e[0;32m+ \u @ \w\e[m\e[0;33m »\e[m '
if [ "$color_prompt" = yes ]; then
    PS1="\[\033[01;36m\]+\u@\h\[\033[00m\]:\[\033[01;32m\]\w\[\033[00m\]\[\033[01;33m\]\n\$(parse_git_branch)»\[\033[00m\] "
else
  PS1="\u@\h:\w\$(parse_git_branch)\$ "
fi

The result of the bash script looks like this:

Picture 1

kick it on DotNetKicks.com

Technorati Tags: ,,

  1. Git/Github survival guide | Ivan Porto Carrero - pingback on March 22, 2009 at 12:48
  2. Also look into http://github.com/rtomayko/git-sh if you like a fancy bash shell

  3. Updated Powershell Scripts – DevHawk - pingback on April 18, 2011 at 04:49

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackbacks and Pingbacks: