Git version control for WordPress development

If you work with WordPress and you’re a proper geek, you want to keep your code under version control, and you want to do it with Git. Yes?

Well, you’re in luck. Even though WordPress is an SVN project, there’s an official git repository on GitHub. It syncs with SVN every 15 minutes. Yay!

So, you’ve cloned yourself a copy of WordPress:

> git clone https://github.com/WordPress/WordPress.git myproject

But you don’t want to work with bleeding-edge WordPress (you’re not THAT geeky). So, you check out your preferred version, let’s say 3.4.2:

> git tag -l
> git checkout 3.4.2

You get a “detached HEAD” warning, chuckle a little at the visual that gives you, and create a branch (as recommended). You probably want to name it after the tag you’ve just checked out:

> git checkout -b 3_4_2

Sweet! Now you’re ready to finish setting up your local copy of WordPress, and then you’ll be ready to start coding! You’ll probably want to add various files and folders to .gitignore before committing your changes (e.g. wp-config.php and wp-content/uploads), but we’re not going to cover that here.

Next, you might want to push your changes to a remote repository. You can’t push to “origin”, because that’s WordPress proper. So, you might do something like this (or not; there are many ways to skin this particular cat):

> git remote rename origin wordpress
> git remote add origin ssh://me@gitserver//srv/git/myproject.git
> git push --mirror --set-upstream origin

This assumes myproject.git is an empty repo on the server. You can create it like this if necessary:

> git init --bare myproject.git

Now you have two remotes: “wordpress” (formerly “origin”, i.e. the official WordPress repo) and “origin” (your self-managed remote repo for this project). You’ve also used –set-upstream to tell Git to push and pull from “origin” by default.

Note that if you clone from your remote repo later, you’ll need to add “wordpress” manually if you want to change versions, for example:

> git remote add wordpress https://github.com/WordPress/WordPress.git

Now, if you’re still with me, let’s upgrade your local copy of WordPress. Make sure your working copy is clean, and hold on to your Red Bull cans!

[NOTE: this assumes you haven’t made any changes to WordPress itself that you want to keep. If you have, you’re on your own now!]

> git fetch --tags wordpress
> git checkout 3.5.1
> git checkout -b 3_5_1
> git merge -s recursive -X ours 3_4_2

The first three lines shouldn’t need any explanation, but the last one is a bit special. It tells Git to merge the changes you made in the 3_4_2 branch into the 3_5_1 branch, and to automatically resolve any conflicts in favour of 3_5_1. This works fairly well, except for any files that disappeared between 3.4.2 and 3.5.1. You’ll need to delete these manually, then commit the merge.

Finally, upgrade your database and get testing!

HTH.