Tuesday, 26 August 2008

Using git to sync server with laptop

After investigating git for the bioruby project, I started using it on basically every project I run. And what do I use it for? Two things: keeping track of changes (duh) and syncing between server and laptop.

I normally try to get IT so far to let me mount my server Documents folder on my laptop when I'm at work. So ~/Documents actually points to my network drive. That's nice, because I don't have to bother with keeping track of several places to store my documents. If I change anything on my network drive, it looks like it's been changed locally. And vice versa.

But: what if I'm at home (where I work just a bit more than the wife would like)? I can still SSH into the server and do some work, but I can't mount that network drive. So I started creating a ~/LocalDocuments folder on my laptop in which I copied any files I needed. But that obviously feels wrong as I now have more than one place to put my files: either on my network drive (~/Documents) or locally on my laptop (~/LocalDocuments).

...until I started using git...

When I start a new project, I create a new folder on the server: ~/Documents/Projects/some_new_project. Within that folder, I run "git init" and commit a README file. This creates the git repository. Next thing: clone it on the laptop.

On the server:

mkdir /path_to_directory/some_new_project
cd /path_to_directory/some_new_project
git init
touch README
git commit -a -m "First commit"

On the laptop:

git clone ssh://my_name@network_server/path_to_directory/some_new_project

Now I just work in ~/LocalDocuments, commit all changes in my local git repository and (very important:) push it back onto the server.

git push

NB: This setup has already saved me from not a small (well: medium) disaster. For some reason (no coffee yet?) the very first thing I did one morning was login on the server, go to the project folder I had been working on for about a month, and do a "rm -r -f this_project". Aaargh! After wiping away all that cold sweat I realized I only had to clone the repository on my laptop back onto the server.