Tracking WordPress using Git

Update: I don’t do this anymore.

I publish this blog through WordPress, for reasons I’ve outlined before. I run it with a custom theme and a bunch of plugins though, and I wanted a convenient way to keep my WordPress install up to date without having to reinstall everything all the time. I wanted source control for my blog install.

My first attempt involved mirroring WordPress SVN into a Git repository on github so that I had a Git version of the SVN tree (including branches, tags and every checkin separate) and seperate repository holding the changes I’d made for my web site. This eventually failed for two reasons, first the script I was using to mirror the SVN into Git had a habit of failing in bizarre ways and secondly having two repositories confused me.

Yesterday I decided to update my fairly outdated WordPress install, it had been missing security fixes for some time and was one minor version behind. Since tracking SVN hadn’t worked I tried a simpler approach, a single Git repository containing a master branch that tracks releases and an ianloic.com branch to track the state of my site.

I set up master with a fresh download of WordPress 2.5 from wordpress.com, created my ianloic.com branch and applied the differences between my site and the 2.5 SVN tag (for all it’s failures my old approach at least let me do this easily). I switched back to the master branch, deleted all the files (leaving my .git directory intact) and unpacked the new WordPress 2.6.1 tarball. I checked that in (to the master branch), tagged it 2.6.1 and then merged that into my ianloic.com branch. I pushed all that to github and then checked it out on my web server (at Dreamhost).

Normally with Git you’re tracking just the master branch, but I want both master and ianloic.com branches to be tracked so my .git/config contains:

[remote "origin"]
	url = git@github.com:ianloic/wordpress.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
[branch "ianloic.com"]
	remote = origin
	merge = refs/heads/ianloic.com

Now it’s easy to track changes that I’m making to my site and update to the latest WordPress without risking losing anything. The process for updating to a new WordPress release is:

  • on my laptop check out the master branch
  • rm all the files except for .git from the directory
  • unpack the new release into the directory
  • git-add . — now git-status will indicate what has changed, been added or removed
  • git-commit to check in the new version of wordpress
  • git-tag versionnum to tag which version is currently in master
  • git-checkout ianloic.com
  • git-merge versionnum to merge the latest version into the site’s branch
  • git-push –all –tags to push all the branches and tags to github
  • on my web server, git-pull to update to the latest release

I end up with a tree that looks like this:

8 replies on “Tracking WordPress using Git”

  1. This is exactly what I’ve been thinking of doing for my site! I had no idea where to start though, and still need to learn how to use git. It’ll probably be easier since I’ll be starting from scratch. Would you recommend github as a repository source? I’m not sure if I want to open source everything that will go on my site for security reasons. Are there any alternative free git providers that allow private repos?

  2. @TrinitronX, git’s a little confusing till you get used to it.

    I like github, but there are some good alternatives. If you want to keep stuff private you can get a premium github account for that, or you can just use any unix account. If you have ssh access you can use that as a git repository. If you want a web gui there are couple you can run on top of a self-hosted git repo too.

  3. Good article. One question. How do you track the wordpress database changes? If you use a bunch of plugins that modify the database and then move to a new version of WP that also modified the DB (let’s say significantly) then what problems you will encounter. One idea I thought was to take a mysqldump with every commit, but manual merging of mysqldump(s) is not trivial either during a rebase or merge.

  4. I am wondering if you would know how to create a custom WordPress installation ( with custom plugins/themes/directories ), and store it on GIT, so I can grab it from command line, and not have to install all the plugins/theme and make all the settings every time?

    1. @Jerry,

      I’m not sure what the best approach to that would be – perhaps maintaining a wp-content directory template (with the right plugins & themes in there) would be your best bet.

      With WordPress’s new upgrade system I’ve given up on maintaining the source in git.

Comments are closed.