One of the legacy problems with WordPress is it does not offer an easy out of the box solution for using Git. This means that it is often the case that theme management is handled through FTP.

Using Git for deployment is far superior than FTP in just about every way. In this post I will show you why you should only ever deploy with Git, and how easy it is to set it up.

Why you should use Git?

I’ve already covered why you should use Git in the past, so I will keep this to a brief summary.

Git is a version control system that sits discretely in the background of your project and monitors any changes you make. After you make a change to your code you can commit your changes to Git which makes a new entry in your project’s timeline. This gives you the ability to go back in time and resurrect your code as it was at a certain point in time.

Git is also invaluable when you are working as part of a team of developers or when you are working on a project that has many concurrent development features or versions.

One of the big advantages of using Git is when it comes to deployment. As you probably already know, using FTP for deployment is not very good. FTP requires you to drag and drop files into their correct folders on the server. This means if you need to update a website that has files that are changed in many different folders, it becomes quite difficult. You also face the risk that an unsuspecting user will load a page that breaks because you haven’t finished uploading all of the new files.

Git allows you to manage deployment from the command line. This means you can push a new update and all of the files that are changed are instantly updated at the same time.

It also means that should you push a new update that critically breaks your website, it is really easy to rollback to the previous version. This would be almost impossible using regular old FTP.

So hopefully you can see the many advantages of Git when it comes to deployment. Again, Git will improve your workflow in a number of different areas which you can read more about.

How does Git deployment work with WordPress

There are many different ways you can handle Git deployment using WordPress. Personally, I only use Git for theme deployment, rather than having it manage my entire WordPress installation. This is because I don’t trust open source plugins enough to compromise my deployment. For example say a plugin makes a change to a file on the live server. The next time I push a deployment my files will overwrite this change.

If you do want Git to manage your entire installation, that’s fine. This is a good set up because you can update plugins locally to ensure nothing breaks before it ever hits the live server.

So the overview of how my Git deployment works is:

1. Local repository

Firstly I have my WordPress installation set up locally so I can develop themes and make changes without affecting the live site. Making changes to a live site is a really bad habit that you need to get out of. I have my Git repository set up in /wp-content/themes/my-theme so only the changes to my theme get managed.

2. Git repository on the server

Next I have a bare repository set up on my server. I like to keep all my repositories under /var/git , but you can keep them wherever you want really.

3. Live site files

And finally, I have my live site WordPress installation under /var/www/path-to-live-site as you typically would with any Linux set up.

So the workflow of making changes is:

Local changes

Firstly we make local changes and commit them to Git

Push to the server

Next we push the changes to the Git repository on the server

Automatically update

When new commits are added to the Git repository, it will automatically update the live theme.

It’s that simple. Easy integration with Git and no more FTP.

Let’s set everything up.

Setting up Git and WordPress

So as I’ve outlined above, there are three main stages to setting up Git to work with WordPress:

Local repository

So the first thing to do locally is simply create a new Git repository. I’m going to assume that you already have Git set up on your machine.

To create a new repository, go to the root of your theme and run: