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 http://rkj.me/a1 > /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:
[alias] 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 http://core.svn.wordpress.org/
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.
I’m probably going to attempt this for 3.3. We’ll see.
Did you or are you planning to?
No, I haven’t had the time, but if someone catches me near the start of 3.4, there’s a good possibility I’ll set everything up.
Nice writeup!
Was going to ask you to push your repo to github, but I see you already have:
http://github.com/markjaquith/WordPress
😀
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.
I know; that’s what I meant.
Could one use this repo to work on WordPress core and then make svn patches? If so, any pointers? Same as above pretty much?
*that* should come with a warning – “extreme geekese ahead”
That’s a read at least three times.
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.
Great article on using GIT
Nice! I set up a BuddyPress mirror with svn2git (ruby version), it’s easier to maintain: https://github.com/buddypress/trunk
Also, you can use
-s
instead of all-t/-T-/b
😉It would also be nice get back synced https://github.com/wordpress/wordpress
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!
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.
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
GitX for Mac OSX looks cool too!
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?
That way the menu will scroll and be accessible regardless of JavaScript. Otherwise just basic styling.
Thank you SO much. I just can’t stomach SVN, and have fell in love with GIT. This is just what the doctor ordered.
This is most definitely awesome. I too also love GIT, I mean how can you not?! WordPress and GIT are a match made in heaven!
Well, I like them anyway..
I’ve never used git, preferring subversion because it’s the WordPress way.
I’m about to try git using GitHub for Mac: http://mac.github.com/ Fingers crossed.
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.
A bit late to the party here, but I wrote a Git to SVN deployment script for my plugins a few months ago that I find nicer than using git-svn: http://thereforei.am/2011/04/21/git-to-svn-automated-wordpress-plugin-deployment/
Mark, do you accept pull requests via your WordPress GitHub repo?
Actually I like it even better than the wp post admin.
Thanks this clarify it for me. Wasn’t sure as I always have trouble with the menu.
thanks mark…
guzel paylasım tsklerımı sunuyorum..