Want to get more articles like this one? Join my newsletter

How to use git push with WordPress

Automating WordPress deployments is an important aspect of modern WordPress development. It lets you deploy changes to a WordPress site quickly and safely. This lets you focus on more important things like fixing bugs and developing features.

Now, there are a lot of different ways to do automated WordPress deployment. You often hear about continuous integration platforms used for that purpose. But these platforms are complex for just deploying changes to a WordPress site.

If you use Git (which you should!), there’s an easy way to deploy changes to a WordPress site. It’s called “git push”. The name comes from the git push command which you use to do the deployment.

Depends on your WordPress host

Now, “git push” is an amazing way to deploy WordPress code automatically. But there’s one major issue with it. It’s that it’s not something that most WordPress hosting companies support. In fact, most WordPress hosting companies don’t support it.

Here’s an alphabetical list of some of the ones that do:

If you run your own server, you can also setup “git push” yourself. We’ll also look at how you can do that later in the article. For now though, we’ll look at how git push works.

How does git push work?

The cornerstone of using “git push” is a git repository. This is where we’ll store both WordPress code and your own code. This git repository might be local only or you could have it hosted online on GitHub or other such services. These repositories also go by the name of “remote” repositories.

Now, for most of us, a remote repository and a repository hosted on GitHub are essentially the same. But, in reality, a repository hosted on GitHub is just one way to do a remote repository. A WordPress site that we git push to is also a remote repository.

And that’s really what deploying using “git push” comes down to. You’re not pushing code to GitHub like you normally would. Instead, you’re pushing your code to a remote repository which is also your WordPress site.

How to set up git push with a WordPress host

With a WordPress host like the ones mentioned earlier, you don’t have to create a remote repository. They’ll either have one created for your site already or they’ll show you how to create one. Once that’s done, they’ll ask you to do one of two things.

Cloning a repository

Either, you’ll have to clone your site’s remote repository. You can do that with the git clone command. You can see the one used to clone a Pantheon site repository below.

$ git clone ssh://codeserver.dev.xxx@codeserver.dev.xxx.drush.in:2222/~/repository.git my-site

One thing to keep in mind is that using the git clone command will set your site’s remote repository as the origin. An origin is the default remote repository used by git. So if you run the git push command, you’ll automatically deploy your code to Pantheon.

Most of the time, this isn’t an ideal situation. Most of us would rather that git push pushed code changes to GitHub by default. If you want to do that, you can just rename the origin to something else like this.

$ git remote rename origin pantheon

So the command above renamed origin to pantheon. This means that if you want to push your changes to Pantheon, you now need to do git push pantheon. This is a bit safer and prevents you from pushing changes by mistake. Afterwards, you can add your GitHub repository as your origin if you want.

$ git remote add origin https://github.com/username/repository.git

Adding a remote repository

The git remote add command is a good way to bring up another way to set up “git push”. You can just add the remote repository for the WordPress site to an existing local repository. This is how WP Engine wants you to set up “git push” for WordPress sites that they host.

$ git remote add development git@git.wpengine.com:production/DevEnvName.git

This adds a remote repository for the development WordPress site. They also have one for staging and production. You can add whichever you want, but you’ll need at least the production one to deploy your WordPress site.

How to set up git push yourself

This covers how to set up “git push” on WordPress host. But what if you have your own server? How can you set up “git push” on it? Well, it’s actually not that complicated!

As we mentioned already, the way “git push” works is that you push your code to a remote repository. So configuring your server to support “git push” comes down to creating a repository on it. To do that, you’ll need SSH access to the site and have git installed.

$ ssh user@server
$ cd /path/to/wordpress/site
$ git clone https://github.com/username/repository.git

First, you’re going to SSH to the server. Once there, you want to use the cd to go to the directory where you installed your WordPress site. Once there, you want to clone the website’s repository.

$ git remote add production ssh://user@server/path/to/wordpress/site

After that’s done, you want to add the cloned repository as a remote repository like we saw with the managed hosts. You’ll notice that the URL that we use to do that is a bit different from our earlier example. Let’s go over each part of it.

ssh:// tells git to use SSH instead of an HTTP connection to access the repository. Because we’re using SSH, you need to specify the user@server that it’ll use to connect to the server. And finally, you need it to give it the path to where you cloned the repository which was /path/to/wordpress/site.

A great stepping stone

The beauty of “git push” is that it’s pretty simple. You just have to use git as you would normally and just use a slightly longer git push command. There’s no need to automate deployments or set up a continuous integration workflow.

On top of that, if you’re using a host that supports it, it takes a few minutes to set up. And even if you don’t, it’s not that hard as long as you have access to your server. So there’s very little to hold you back if you want to get started!

Photo Credit: Yancy Min

Creative Commons License