Version Controlling WordPress
by Roy Barber, 27th January 2013A lot of friends of the web asked how i usually work with Git + Wordpress, so i wrote this to show the structure and steps I take to setup Wordpress locally, version control and then deploy to a live server with Git. Avoiding the need for 'Cowboy Coding' and a fully backed up version of your website.
Wordpress & Git
This is what we are aiming for:
I'm presuming you already know how to use Git, if not follow this guide and you should be upto speed in no time.
I use MAMP Pro to manage all my local versions of sites, if you use something different then the below might be setup slightly different but still with the same aim.
First i locate my local htdocs folder and create a new folder, naming it appropriately. This will be the start of the repository. Inside the folder i then create a further two folders called htdocs & sql. htdocs contains all the Wordpress files and sql always has the latest version of the database, so this can also be version controlled.
Why? because I want my .git folder to be sat behind my website root and not accessible on my live server, preventing people from going in and grabbing code & passwords.
Now into MAMP Pro to setup the local version, creating a host and pointing it to the htdocs folder within my future repository. I usually name my local urls like so client.projecttype.dev example roybarber.website.dev.
If you don't have the ability to create a host, or run MAMP not MAMP pro you could just use http://localhost/client/htdocs/ to view the local version, and use that url within your Wordpress setup.
Next download and install wordpress like you usually would, ensuring you place the files in the htdocs folder.
Next thing is to setup a local settings and live server settings file, so Wordpress knows which version it should use depending where it is.
Create a file in the root of wordpress (where wp-config.php is located) called wp-local-config.php this will house the local database and url settings. Heres an example of its contents:
Next open up wp-config.php, we're now going to tell it to use the local settings if they exist, otherwise use the live server settings, heres an example of its contents:
So hows this going to work i hear you say! well we don't want the local settings file to get onto the live server, so we simply just make git ignore it by creating a .gitignore file in the root of the repository. Heres its contents:
Go ahead and create the file and edit where needed.
Ok so now we should have a fully working local version of Wordpress setup ready to start development on.
A Quick note on SASS - if your using sass/compass to compile stylesheets then i highly recommend ignoring the /.sass-cache/ folder as this will always differ on the server to locally and save you a world of hurt later! just edit the .gitignore with the name of your theme name before you add the files to the repository.
Lets get it into git! open up a terminal cd to the folder you created (housing the htdocs and sql folders) and initiate git. (~ git init) now git is initiated and tracking changes on files, its a good time to add the files to a repository and push a version up.
Now just go ahead and develop the website as usual, but doing frequent additions to git and pushing to the repository. Add the pages and content etc then when your ready to push to a server take a dump of the sql file.
Deployment with git
So now to start deploying the Wordpress install to a live server, I'm presuming you have ssh & root access to the server, if not you probably don't have git setup or the ability to deploy the files via ssh.
To start i simply ssh into the servers htdocs and do a git clone, pulling down a copy of the repo. Then create a new host pointing it to the htdocs folder where Wordpress is. You might do this via a control panel, but the most important thing is to just make sure the website knows where the files are.
Then just import the database to the live server making sure the settings of the database match the ones you put into the wp-config.php file earlier. No need to change the url's as its all in the config. If you get a can't connect error then you probably didn't ignore the local settings file and need to tell git to ignore it manually, a quick search will show you how.
There you go, a fully version controlled copy of Wordpress ready to tweak and update locally, then deploy to the live server when your happy it works. Personally i don't touch the site once its live, just make the changes locally, update the repository and pull down the latest version on the server. Then just a quick export and import of the database.