Distributed programming using Git

I am being used to programming on *nix systems for many years. I quite early discovered the interest of using source code managers (SCM's) such as CVS at first, then Subversion (also called SVN) when it imposed itself as a valid replacement for CVS.

I used them a lot, first for simplifying some collaborative working tasks, such as code sharing and history keeping, and second for home projects, in a easy way for backup, history tracking, and working on the same set of files on different computers at the same time.

This came to become seriously problematic when I tried to use Subversion for both collaborative work, and centralizing my work among different computers.

And then, Git appeared, and it solved all my problems.

This article deals with using Git as a way to synchronize changes among different computers (from the same user), and to push these changes when necessary to a remote collaborative SCM repository, whether it is a Git or SVN.

Create a central repository

First of all, you need to centralize your changes to that collaborative project. This is done by creating your own Git repository, on a computer to which you have remote access to, from any of your different workstations.

On this remote computer, create an empty Git repository. For this, from the directory you want the repository created into, simply issue the commands:

mkdir myproject.git
cd myproject.git
git init --bare

That's all.

Create a working repository on your workstations

First time

First, create a local clone of the upstream repository with the command:

git clone url_of_upstream

On the first machine you clone the original repository, you need to create a branch which will contain your changes against the original sources. Only the changes made to this branch need to be shared among your workstations.

The command for creating a new branch (called 'mybranch' in the example) from an upstream 'thebranch' branch is:

git branch --track mybranch origin/thebranch

Now set your central repository as a new remote object (called myrepo):

git remote add myrepo url_of_your_repo

Push your new branch into your central repository:

git push myrepo mybranch

Modify the local branch so that it now tracks the central repo:

git branch --track -f mybranch myrepo/mybranch
git checkout mybranch

That should do it.

Subsequent times

Once the branch has been pushed into your central repo, the commands for fetching the project on another machine are:

git clone url_of_upstream
cd projectname
git remote add myrepo url_of_your_repo
git fetch myrepo
git branch --track mybranch myrepo/mybranch
git checkout mybranch

Pushes and pulls are now done by default to your central repo.

You can pull the changes from the upstream branch (whose name is thebranch, as in the example above), with the command:

git pull --rebase origin thebranch

If you ever want to push your changes to the collaborative repository, use the command:

git push origin mybranch:thebranch

Which will do the job, provided you have write access to the upstream repository.