Pushing WordPress Theme Updates With Git

jackharner

Jack Harner 🚀

Posted on September 11, 2018

Pushing WordPress Theme Updates With Git

Do you frequently update WordPress themes? It's kind of a pain in the ass, right? Typically has to be done manually through FTP or uploading a new zip through the backend.

Well, today I'm going to show you how to update your site with just:

$ git push live
Enter fullscreen mode Exit fullscreen mode

Sounds to good to be true? It's not. Check it out:

Prerequisites

1) SSH Access to your web server
2) Git installed on your local machine and on your web server

This setup works best for one off customs themes (like the one on HarnerDesigns.com) and not for themes produced to be distributed.

Create Your Repo Locally

First things first, create a folder for your project and enter it:

$ mkdir git-test && cd git-test
Enter fullscreen mode Exit fullscreen mode

then initialize the repo:

$ git init
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can just clone my Blank2 - Blank WordPress Theme repo to get started:

$ git clone https://github.com/harnerdesigns/blank2.git git-test
Enter fullscreen mode Exit fullscreen mode

Create A Bare Repo On The Remote Server

A bare repo is a directory containing the working tree of your git project, just without storing all the files. It's essentially just a directory of all the files noramlly in your .git directory.

SSH into your server:

$ ssh <user>@<server>:<sshPort>
Enter fullscreen mode Exit fullscreen mode

Create a folder for the bare repo and enter it:

<user>@<server> $ mkdir git-test.git && cd git-test.git
Enter fullscreen mode Exit fullscreen mode

Remember where on the server you created this directory, you'll need that later (Doing it in your home directory works perfectly fine).

Initialize the bare repo:

<user>@<server> $ git init --bare
Enter fullscreen mode Exit fullscreen mode

Hook It Up

Your shiny new bare git repo has a directory hooks and in it are a bunch of .sample files. We want to do something after the repo receives new data, so create a new file called post-receive (no extension) and add the following to it:

#!/bin/bash
GIT_WORK_TREE=<pathToWordPressInstall>/wp-content/themes/git-test git checkout -f
Enter fullscreen mode Exit fullscreen mode

What this will do is, any time you push to this git repo, set the working tree for the repo to the theme folder in your WordPress install and then force checkout the files.

Back Down From The "Cloud"

Now logout of your remote server. It's time to add the new repo as a remote for your local one.

Make sure you're in your project's folder:

$ git remote add live ssh://<server>/home/<user>/git-test.git
Enter fullscreen mode Exit fullscreen mode

and that's it! All you need to do to update your live theme now is add some files, commit them like normal and then run:

$ git push live
Enter fullscreen mode Exit fullscreen mode

Being able to Update WordPress sites quickly allows for quicker bug fixes and less time spent wrangling files. Combine this idea with a taskrunner like Gulp and you'll be flying through updates.

Let me know if you use this method or if you have any other ways to manage WordPress Themes!

More Of My Writing

💖 💪 🙅 🚩
jackharner
Jack Harner 🚀

Posted on September 11, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Pushing WordPress Theme Updates With Git
wordpress Pushing WordPress Theme Updates With Git

September 11, 2018