Git without a Central Repository

In my mind, the way git is supposed to work is that two people have git repositories and they pull and push code to each other. In the organization where I work, we also need to push code to live web servers. I imagine those servers as if they are just other git users.

There is a tenancy to want to use a complex central repository system such as gitosis or github. These systems are good for a central backup but they also make the process more complex and aren't strictly necessary. Instead, you could just create the git repository on the server, initialize it, and then create a branch.

Two git users can push and pull data to each other, with one minor gotcha. You cannot push data to a remote repository that already has the same branch checked out.

One simple solution is to create two branches on the web server. The master branch and a live branch. Then, checkout the live branch on the production server. With that done, all other git users are free to clone, pull, and push to the master branch or to any other branch besides the live branch.

Finally, one last step is necessary. On the server you'll want to setup a cron job or a hook that merges the master into the live branch. Merging from master to live is one simple command that you run from inside the directory where your git repository is setup.

git merge master

If I were to set this up in a cron job, here's what my cron job might look like.

* * * * * cd /project/directory/; git merge master

You can also setup hooks in git and doing so is probably better than running this in a cron. On the flip side, doing a merge from two local branches is probably not that expensive either.

Creating a Repository on the Server

Here's the process for creating a new repo on the server that will ultimately host the files. This is a standard git repo setup.

cd /project/directory/
git init
git add somefile.ext
git commit -m 'Initial commit.'

Now we have our master git repository we also need to create a live branch and switch to it.

git branch live
git checkout live

Checking Out from a Workstation

Now, back on my workstation, I'm ready to checkout the master copy from the server. This is just a standard git clone.

git clone ssh://

If you get any permissions errors, make sure that you have read and write permissions on all the files in the repository including the .git directory and the files it contains.


comments powered by Disqus
git_without_a_central_repository.txt · Last modified: 2012/02/07 15:44 by Joel Dare