Upgrading a Drupal site with Git

If you have many Drupal websites on your server, you might want to use a Git repository to ease the upgrades. There is a Drupal repository on GitHub at https://github.com/drupal/drupal.git that contains all the releases and is up-to-date with the development CVS used at Drupal.

Prerequisites:

  • If you don’t have yet a local Git repository for Drupal on your server, these are the steps to clone one in /var/git/drupal:
    mkdir /var/git
    cd /var/git
    git clone https://github.com/drupal/drupal.git
  • We’ll assume you place your Drupal website in /var/www/$SITE/drupal.

In this article I’ll show you how to upgrade a Drupal website easily, thanks to Git (on a Linux server).

Upgrade your local Drupal Git repository

It’s a simple task you could run regularly in a cron job:

cd /var/git/drupal
git pull

Prepare your upgrade

In this example we’ll suppose you want to upgrade from Drupal 6.22 to Drupal 6.23 — of course, you should set the following variables to your configuration:

export VERSION=6
export CURRENT_RELEASE=22
export CURRENT=drupal-$VERSION.$CURRENT_RELEASE
export NEW_RELEASE=23
export NEW=drupal-$VERSION.$NEW_RELEASE

This is the website I’m upgrading — Change to your configuration accordingly:

export SITE=notes.tela-group.com    Name of the directory holding the website
export DB=notes_telagroup            Name of the database

We’re setting some additional variables:

export DATE=`date -I`

Some backups

cd /var/www/$SITE
ls -l
  • Backup MySQL
    mysqldump -u root -p $DB | zip > $DB.sql.$CURRENT.$DATE.zip
    ls -l
  • Commit changes

Look at the Git status to check if you made some changes which were not committed:

cd /var/www/$SITE/drupal
git status

If positive, do a commit:

git commit -m "`eval echo $SITE $CURRENT online`"

The upgrade

Now it’s time for the upgrade. The Git command ‘merge’ will do it:

cd /var/www/$SITE/drupal
git merge DRUPAL-$VERSION-$NEW_RELEASE
git status

To look at the changes, use the diff feature found in Git. This will list the differences made by the Drupal team:

git diff —theirs

To see the differences you’ve made since the last merge, run this command:

git diff —ours

I usually put a comment with my name “ericdes” or “tela” on all the change I make to the code, this allows me to retrieve them quickly with these commands:

git diff —ours | grep ericdes
git diff —ours | grep tela

Resolve the conflicts

You’ll probably face some conflicts, here’s how to resolve them (3 methods):

  • Manually
    export file=CHANGELOG.txt    Replace with a conflicting file name
    nano $file                 Correct the conflicts

    git add $file
  • Overwrite with our release
    (git 1.6.1 and up)
    export file=sites/default/settings.php   Replace with a conflicting file name
    git checkout —ours $file
    git add $file

    (or)
    export file=sites/default/settings.php
    git reset $file
    git checkout ORIG_HEAD $file
    git add $file

    (or, use our script)
    ~/git-overwrite-with-our-release sites/default/settings.php
  • Overwrite with their release
    (git 1.6.1 and up)
    export file=CHANGELOG.txt     Replace with a conflicting file name
    git checkout —theirs $file
    git add $file

    (or)
    export file=CHANGELOG.txt
    git reset $file
    git checkout MERGE_HEAD $file
    git add $file

    (or, use our script)
    ~/git-overwrite-with-their-release CHANGELOG.txt includes/cache.inc includes/common.inc

Do this for all conflicting files, and verify:

git status

Once all conflicted have been resolved, you want to commit:

git commit  -m "`eval echo $SITE merged with $NEW`"
git tag -a \
     "$SITE-$NEW" -m "`eval echo $SITE $NEW`"

Now it’s time to update the database:

php update.php

Check that your website is running the new Drupal version!

Tags:

Add new comment

Wiki Textile Syntax

  • You can enable syntax highlighting of source code with the following tags: [code], [blockcode], [asp], [linux], [c], [cpp], [c#], [delphi], [dos], [f#], [html], [ini], [java], [javascript], [mysql], [perl], [php], [postgresql], [python], [ruby], [sql], [text], [vb], [xml].
  • You can use Textile markup to format text.
  • Web page addresses and e-mail addresses turn into links automatically.

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.