A Git Setup for WordPress

As I mentioned in my previous posts about how to setup git deployment on a staging and a live server for a WordPress site, setting up a repository for your WordPress site is not a complex task.

Let’s review the steps to do it.

First of all, you need to realize that you don’t want to version anything but your custom code. WordPress and the plug-ins you are using are already being versioned by their respective authors. To do this, your workflow needs to start from your local machine.

You need to have a local development environemnt setup to test your features and work swiftly on breaking changes.

Step 1. Setup your git repository on github

You need to create a new repository, and clone the repository on your local machine. I like to do this using GitHub’s desktop app.

Step 2. Setup your repository locally

Now create a directory structure for your repository. Remember we want to leave out all the WordPress core and 3rd parties plug-ins: these are already versioned eslewhere.

My typical WordPress repository consists of the following directories:

  • root level → support scripts that reside at the root level of the site (i.e. .htaccess)
  • wp-content/plugins/ → site-specific custom developed plugins
  • wp-content/themes/ → site-specific custom developed themes

Optional directories include:

  • misc/ → other stuff that doesn’t bleong to the above groups
  • assets/ → images, css, javascript, etc that do not belong to wordpress but you still need to store on your webserver

Step 3. Setup your local web server

In my local environment, I currently use MAMP as web and PHP server. I create an arbitrary root directory for the server where I unzip a fresh copy of WordPress.

Then, I download a database dump from the live server and import it locally. If the database is very large, there are better ways then doing this with phpMyAdmin. Be sure to Search and Replace your live Domain with Localhost in the WordPress Database.

Setup your wp-config.php to use the database you just populated.

I usually also download a full copy of the wp-content directory from the server, so that I will have locally all the plug-ins, themes and images exactly as they are served from the server.

Once you move these in place, be sure to keep in the wp-content direcrory of your local web root all 3rd party plug-ins and themes.

Setp 4. Move custom code in the repository

The custom plug-ins and themes instead belong to the appropriate directories in your repository. Move them out of the wp-content directory of your local web root and into the wp-content directory you created in the repository.

Step 5. Make the local site use the custom plug-ins and themes from the repository

Now, how do you connect all the stuff in the repository to your local web server root?

You could keep everything mixed together and build a huge ignore file, but I personally prefer to create “links” of the custom plug-ins and themes, so that I keep everything clean and separated.

The fastest way to do this is with the Terminal app. It’s one command for plug-ins, one for themes, and works as follows:

ln -s ~/path/to/your/repository/wp-content/plugins/* ~/path/to/your/local/web-root/wp-content/plugins/

ln -s ~/path/to/your/repository/wp-content/themes/* ~/path/to/your/local/web-root/wp-content/themes/

If, in the future, you will add a custom plug-in or a child theme to your repository, you need to remember to create a new link for it, i.e.:

ln -s ~/path/to/your/repository/wp-content/plugins/new-plug-in-name ~/path/to/your/local/web-root/wp-content/plugins/