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.

5 comments:

xyz said...

For what its worth, there is a way to mount a folder (effectively, the entire partition for that matter) on a remote box through a ssh connection (http://fuse.sourceforge.net/sshfs.html).

Git is primarily a versioning tool, and its great, if that is what you want. On the other hand, if you just need to keep the files in sync, rsync is the right tool. Example:

rsync -avz -e ssh login@remote.server:/home/login/remote-dir local-dir

This command would recursively sync everything from the dir on the remote server to the local directory, while keeping the ownership, timestamps, permission and compress the data for the transport line.

Christoph said...

You might be interested in Gibak on eigenclass.org

Jan Aerts said...

@xyz: True indeed. rsync does the job as well and has always been the number-one tool for that. However, I'm not sure if it works across operating systems (well: I'm lucky with a OS X laptop and a linux server, but what if I'm on Windows?). Also, I keep a lot of version information in my script directories as well. So this approach helps me to both sync _and_ do version control.

@Christoph: I had bumped into gibak earlier but didn't look into it any deeper. Will do that.

Anonymous said...

I like git and RCS in general very much.
In my laboratory, we would like to set up a Trac server (http://trac.edgewall.org/) to have a web interface for our git projects and a wiki/ticketing system.

Github is also very useful and I have many projects hosted on it.
The only thing it misses is a bug/tracking or ticketing system, which sometimes could be useful to scientists, too.
There is also this hosting service called assembla (http://www.assembla.com/) which seems very interesting, but at the moment it seems they are having problems with git-based projects.

gioby said...

ehi, well, it was me :=)

Post a Comment