git completion on MacOsX

For most of the day I work on a linux machine and I quite got used to having git completion feature for free. But as an occasional MacOsX user I felt quite bad not having the same functionality available. So yesterday I spent a couple of hours googling and playing until I finally got it all working on my MacBook.

Here’s a quick guide.

First of all, you need git-completion script. You can either get it from git source archive

#> curl http://kernel.org/pub/software/scm/git/git-1.7.1.tar.gz -O
#> tar -xzvf git-1.7.1.tar.gz
#> cp git-1.7.1/contrib/completion/git-completion.bash ~/git-completion.bash

or download it directly from github

#> curl http://github.com/git/git/raw/master/contrib/completion/git-completion.bash -O
#> cp git-completion.bash ~/git-completion.bash

Next, modify your ~/.bash_profile file by adding the following line to it:

source ~/git-completion.bash

Now restart you terminal and start using TAB key to autocomplete git commands!

But I didn’t stop there. When I enter a directory with a git repository I like to know which branch it is currently on. To achieve that all you need to do is to modify PS1 environment variable. Add the following line to  your ~/.bash_profile file:

PS1='\u@\h:\W $(__git_ps1 "(%s)")\$ '

Now restart you terminal and cd into a git repository.

sebmarek@proofek:~ $ cd ~/git/phpUnderControl
sebmarek@proofek:phpUnderControl (master)$

To make it even more visible I use bash colouring feature to highlight branch name in a colour of my choice. See bash prompt HOWTO for more details. That’s how my PS1 environment variable looks like:

PS1='\u@\h:\W \[\033[32m\]$(__git_ps1 "(%s)")\[\033[0m\]\$ '

and that’s the result:

sebmarek@proofek:~ $ cd ~/git/phpUnderControl
sebmarek@proofek:phpUnderControl (master)$

Happy giting!

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: