How I use Git for my Drupal sites

Submitted by Rich on 4 November 2011 - 10:38pm

I have a server with several Drupal websites on it. I want a separate codebase for each so that I can tweak, play on one without affecting the others. I like using Git. This page documents (for my reference as much as for your delight!) my workflow.

The + in the fetch refspec means that source refs will overwrite destination ones even if it's not a fast forward update. There is a danger here that if I create a branch with a name used (now or in the future) by the origin, then my branch would be overwritten and lost. But as long as I stick to domain names for branch names, I figure I'm fairly safe.

Setting up a new site (e.g. at Drupal 7.8)

The clone is fast and space efficient thanks to hard linking (assuming it's on the same partition as the bare repo).

git clone /path/to/bare/repo newsite

I then create a new branch with the name of the site, from the stable tag:

cd newsite; git checkout -b newsite 7.8

And finally, I push this branch onto the origin

git push origin newsite

Setting up a developement server for the site

It is possible to specify a branch at the point of doing the clone. This means you local master branch points to a differently named branch on the remote. I find this leaves room for ambiguity and error, which is why I prefer to create named branches.

Normal development

Work on the development server is committed locally, then pushed up to the bare repo'. This is easily pulled down into the live site.

Dev:sitebranch → Bare:sitebranch → Live:sitebranch

Likewise this process occurs in reverse if I do a quick fix on the live site. And I always pull before attepting to push, of course, because you can't resolve conflicts in a bare repo.

As my development server does not have a fixed IP, I would not be able to pull commits directly from dev into my live working trees. So the bare repository, hosted online, enables this process.

Upgrading Drupal, e.g. to 7.9

First, the bare repository needs to fetch all the great work that's gone into Drupal since it last checked.

cd /path/to/bare/repo
git fetch --all

Now our bare repository has all the commits needed for 7.9 (and some) I can pull all the commits since my original checkout (which might be 7.8, say) with