Using Git to synchronise my documents across two computers

I use a desktop and a laptop computer for many of the same tasks, which include coding as well as working with binary files (like Numbers and Pages). In the past, I used RSync to keep the two sets of the same documents synchronised, but came across problems. For example, Rsync doesn't do conflict resolution, it only compares files based on their last date/time of editing. It is also very, very slow. So I looked for a better way to achieve this common and frequent task.

Enter Git.

I use Git to synchronise documents between my desktop and laptop Macs. This is how I do it:

First, I initialised the directory that holds my work documents:

git init .
Second, I commited everything to the new repository:
git commit -a -m "First commit of everything."
Let's call the machine that holds this original repository M1.

The first commit may take a while depending on the size of your documents folder. On my Macbook Pro it took around 10 minutes for 8Gb of content.

You can edit the .gitignore file to exclude anything you don't need.

Now go to the second machine on which we will transfer the repository by cloning the repository on M1. Let's call this machine M2. The two machines will need to be on the same network to make it possible to do the cloning using the commands that will follow. If not, then it is still possible to make this synching method work via the Internet, you will need to adjust the repository URL for M1. Type "man git-clone" for examples of supported URLs.

Make the initial copy of the repository from M1 to M2 like this:

git clone /Volumes/docos/
In this case, I have mounted the documents directory in M1 on M2 by using the "Connect to Server" function of the Mac OS X Finder (Ctrl-K). Again, you can do this without mounting the directory to M2, but you will need to adjust the URL.

As a result, I got something like this:

Checking out files: 100% (6021/6021), done.
That's it, most of the work is done.
Now let's say you made a change on a file on M2, and you would like to sync the change with M2. Simple, once you commit the change to the M2 local repo ("git commit -a -m "A change."), push it to M1:
git push /Volumes/docos HEAD
Verify the update on M1, you'll see it was magically transported much, much faster than what is possible with rsync.

If you make a change on M2, you can update M1 by "pulling" the change:

git pull /Volumes/docos/ HEAD
Very simple. You can use "pull", or "push" as needed to move changes between M1 and M2. Try also "git log" and "git status" for revision information. Also note that pulling also automatically merges the local and the remote branches in case there are non-conflicting changes. If there are conflicting changes, Git will let you know and give you instructions for manually resolving the conflict. This may not be possible for binary files though.

About

Twitter