Saturday, June 14, 2008

Using Git

A year ago I used subversion on a private project - it was the first time I'd ever used an SCM - and I thought what a great idea. Then I had to create a branch and merge in vendor code. Ouch!

After the project I thought about starting to use subversion at work to track various projects, but branching was the most useful feature, and it seemed too hard to do regularly. Git was still quite new then, but recently I looked at it again, and it's much easier to use now than subversion (IMHO).

Git has many advantages for me as my primary development machine is a laptop. I prefer to create a branch for feature work, and this is trivial to do in Git. There are plenty of resources and tutorials out there, but here are a couple of things I found particularly useful.

The first are bash aliases:

alias gst='git status '
alias gc='git commit '
alias gca='git commit -a '
alias ga='git add '
alias gco='git checkout '
alias gb='git branch '
alias gm='git merge '

The second is displaying the current branch in the prompt. If you are making and moving between a lot of branches - and once you start using Git you will - this helps you keep track of where you are.

This tip uses the __git_ps1 function supplied as part of a contributed package.

Copy the git-completion.bash file from the contrib folder in the Git source tree to somewhere like .~/.git-completion.sh, and add this line to your .bashrc file:

source ~/.git-completion.sh

Then change your prompt to something like this:

export PS1='\h:\w\[\033[32m\]$(__git_ps1) \[\033[0m\]$ '

NB: This function will also show the SHA1 of the current checkout if that is not an actual branch.

The included file also allows the following to be auto-completed:
  • local and remote branch names
  • local and remote tag names
  • .git/remotes file names
  • git 'subcommands'
  • tree paths within 'ref:path/to/file' expressions
  • common --long-options
The auto-complete does not work with bash aliases, so I added these lines to my bash file.

complete -o default -o nospace -F _git_branch gb
complete -o default -o nospace -F _git_checkout gco

No comments: