Painless Wordpress updates with Git
Wordpress' code structure makes me cringe every time I have to deal with it. I keep coming back to it because the admin interface is great and the non-technical people who actually use the sites I build get on well with it. This means looking after multiple Wordpress installs, each of which needs upgrading when a security fix is released.
Until now I've been having to do the upgrades in a really slow, tedious
way --- unpack the new release into its own folder, copy everything
install specific to the new version, and then fire up your test
environment whilst hoping you didn't forget anything. Yesterday I
decided to find a better way. I've been using Git to manage my
projects so unsurprisingly my solution is centered around it. I can now
upgrade a Wordpress install with a simple git merge -s subtree
wordpress/master.
Setting up the Wordpress history
First you need to make a Git repo for Wordpress releases:
mkdir ~/wordpress
cd !$
git init
Then get the oldest release you need to support and commit it to the repo you just created. You can find links to all the releases in the Wordpress download archives.
curl http://wordpress.org/wordpress-2.8.1.tar.gz | tar xz --strip 1
git add .
git commit -m "Version 2.8.1"
Now go through all the newer releases and commit them one at a time until you have a full version history.
rm -r *
curl http://wordpress.org/wordpress-2.8.2.tar.gz | tar xz --strip 1
git add -A .
git commit -m "Version 2.8.2"
Setting up your project
Find out which version of Wordpress your project currently has (line 11 in wp-includes/version.php should have it). Now get the sha1 for the commit with that version (it should be a 7 character hex code):
git log --oneline
Create a new Git repo to hold your install-specific code:
mkdir ~/my-project
cd !$
git init
echo "Wordpress site for my-project" > README
git add README
git commit -m "Initial commit"
Pull in the specific version from your wordpress repo:
git remote add -f wordpress ~/wordpress
git merge -s ours --no-commit 8ef0fdb
git read-tree --prefix=htdocs/ -u 8ef0fdb
git commit -m "Added Wordpress codebase"
Your repo is now linked to the Wordpress repo you created earlier. All that remains is bringing in your existing content:
rm -r htdocs/
cp -r ~/existing-project/htdocs htdocs
git add -A .
git commit -m "Imported existing content"
Double check everything is working as expected at this stage and then go ahead and upgrade the version of Wordpress to the latest.
Doing an upgrade
Update your Wordpress versions repo with the new version as you did before, then head to your project directory and pull in the changes:
git fetch wordpress
git merge -s subtree wordpress/master
If there are any conflicts (most of the time there shouldn't be) Git will tell you where and leave you to resolve them. How easy is that?