Developing on WordPress using Git

WordPress uses Subversion (SVN) for revision management. Before Subversion, it used CVS. Right now, Git is a hot option in the SCM category. It offers really nice features such as decentralization, speed, fast and cheap local branching, better merging, more offline capabilities, staging of commits, and lots more. It’s premature to talk about moving WordPress core and plugins to another SCM system — we have a lot invested with Subversion and Trac. But be of good cheer. You can have your Git and commit to Subversion too! Here’s how I do it.

First, tools. You’ll need Git, obviously. But you’ll also need git-svn-diff, a Bash script that generates Subversion-compatible diffs.

Download git-svn-diff, put it somewhere in your path, and make it executable. Like this:

curl -L > /usr/local/bin/git-svn-diff
sudo chmod +x /usr/local/bin/git-svn-diff

Next, to enable you to do git svn-diff instead of git-svn-diff, edit ~/.gitconfig and add this:

	svn-diff = !git-svn-diff

This next step is going to take a while. You’re going to pull down WordPress’ SVN history using Git’s SVN support.

git svn clone -t tags -b branches -T trunk

You might want to let that run overnight. Really. It’s going to go through each changeset.

Once you’re done, you should be in the Git master branch, which corresponds to WordPress SVN’s trunk. WordPress’ branches are in remotes/{name}

To pull in the latest changes from SVN, use git svn rebase. Important rule: never modify the SVN branches (remotes/{name}). Instead, create a new topic branch.

For example, say that I’m going to work on a ticket for trunk. I’d create a new branch from remotes/trunk like this:

git checkout -b ticket-12345 remotes/trunk

That will create a new local Git branch called ticket-12345 based on SVN’s trunk, and then check it out (i.e. switch to it).

If you’re working on a WordPress SVN branch, you can do something like this:

git checkout -b ticket-12345 remotes/3.1

Do your work in the branch you created. You can make multiple local Git commits if you want, to break up your work into smaller chunks that make sense to you.

When you’re ready to submit your patch, use git-svn-diff to produce it.

git svn-diff > ~/12345.diff

If you have commit access, you can commit to Subversion from this topic branch. But be careful! First you should do git svn rebase to bring your patch up to date. Next, you should squash your local git commits, otherwise each one of them will be individually committed to SVN (hello, flood). So rebase your commits into one commit, like so:

git rebase -i remotes/trunk

Use “reword” on the first commit. Use “fixup” on the subsequent ones. That will roll the commits up into one. You’ll then be prompted to enter your amended commit message for that commit amalgam.

Ready? You can now commit to SVN using:

git svn dcommit

Git knows which remote SVN branch it came from when you checked out your topic branch. You can verify which one it is attached to by doing:

git svn info

A few tips:

Create a .gitignore file. This lists files or directories that you want Git to ignore. First, you want Git to ignore the .gitignore file itself! Next, you want Git to ignore your local wp-config.php Finally, you want to ignore any additional plugins, must-use plugins, themes, uploads, etc. Just do a git status and add anything that you don’t want to commit to WordPress or put in your patches.

I hope you found this helpful! Let me know if you have any questions.

30 thoughts on “Developing on WordPress using Git

    1. That’s not actually my repo… I have tons of local topic branches, obviously. That’s just a Git version of the SVN repo, updated every 20 minutes.

  1. I’ve been using git to version control and distribute development for WordPress and it’s been awesome. The only thing I haven’t figured out is how to do configuration management with the database, like how to only push out a few page updates and not others.

  2. Wow – it’s like every time I have an idea, someone else has the same idea. I was looking up the other day on how to manage my WordPress plugins using git instead of svn or in sync with svn. Thanks for the writeup!

  3. Perfect timing! I’m giving a short talk over at Automattic on how I’m using git and svn with WordPress and plugin development.

    _Thank you_ for pushing your repo to git as well. I realize anyone can do this, but having someone on core do it brings credibility to the table.

    Totally agree it’s premature to talk extensively about moving WP to git, in toto. But… since you mentioned it here… the discussion has been broached.

    Also, since you mentioned it, Trac seems to be an incredibly powerful, yet incredibly underused tool. I’ve been using it for years for personal projects. I don’t think it’s inconceivable: Trac could have the potential to be a “WordPress” for software project management. It would be stupendous if Automattic threw a few developer ducats that direction.

  4. I also use SVN on the job and just last week started github to gain some version-control and keep my child-themes work straight and tidy.
    Once I realized that git is _not_ like SVN I picked it right up!
    Thanks Mark,
    Lee Shelton

  5. Nice writeup… I love git. Most of my friends are rails guys and they got me using Git a long time ago and that’s like all I know…
    Had to learn svn to put up plugins.

    Wondering if submitting and updating a plugin is similarly easy to use git?

  6. Mark, you should include some words about revision limiting with the -r flag and spare the WordPress svn servers some reaming for anyone who ends up wanting to give this a try. Also -s flag can be used now to tell git-svn that the tags/, trunk/ etc. is there in standard format.

Comments are closed.