This guide shows a Git and WordPress workflow and demonstrates version control using Git from a local development environment on OSX to a live production webserver which is a cPanel server running on CentOS.
CentOS cPanel comes with Git and you can do an easy install for OSX.
Set up SSH (no passwords)
A key requisite for this is that you have SSH access to your website which in some shared hosting instances can be limited. The biggest initial hurdle in getting the workflow going is to have passwordless SSH connection by transferring your locally generated public key into the authorised file of the remote server, once this is done and the connection works you are good to go.
In this guide just a regular WordPress theme directory named mytheme inside the webroot; /public_html/wp_content/themes/mythemename is going to be the example. This can be extended to the whole webroot, or any particular directory you want under version control.
Remote Server – Set Up Git Repo
First thing is to set up the server repo, but do it outside of the webroot public_html one level above in the user home. This will contain the version control data and later we will push the actual source files to our WordPress theme directory which will be known as the working directory. So SSH into your server and switch to home…
If SSH is on a non-standard port:
ssh firstname.lastname@example.org -p2000
Make a directory which will store our version control data and initialise it with a -bare option which will have no working directory. This is the way this needs to work and we will push the actual files to our working directory destination when we use the Git hooks.
git --bare init
This will be eventually be our master branch, you should be left with output like so
Initialized empty Git repository in /home/username/wptheme.git/
Local Dev Set Up Git Repo
Now lets set up a local Git repo and add the server repo as our remote. In this local example, the directory will be a local development site in WordPress which already has files already in it.
Add all the files to be tracked (or only the ones you want), the period will add all files or else use the filenames themselves to add instead of the period:
git add .
And commit all the files
git commit -m "first commit"
Check the status and we should have a clean directory
# On branch master nothing to commit, working directory clean
Add the Remote to the Local Repo
Still on the local environment, time to add the remote repo.
The naming might sound non-intuitive, setting up origin but it is more of a destination. Our local repo is master and our remote will be origin/master
git remote add origin ssh://email@example.com/home/username/wptheme.git
Or non-standard port
git remote add origin ssh://firstname.lastname@example.org:2000/home/username/wptheme.git
I found that for cPanel/CentOS when creating the remote you need to prefix the address with the ssh:// protocol.
Push our files and version control data up
git push origin master
You can check the remote URL by running
git remote show origin
Pushing the Server Repo to our Working Directory with Git Hooks
SSH back into your server and make the actual working directory
mkdir wptheme ~/public_html/wp-content/themes/
Move to your server repo and change directory into the hooks directory
Make a new post-receive hook this hook will action once a remote repo has pushed to it, in other words once our local repo has pushed data to it it will execute the action inside – which is to move our latest tracked files to their destination.
#!/bin/sh git --work-tree=/home/username/public_html/wp-content/themes/wptheme --git-dir=/home/username/wptheme.git checkout -f
So here we are declaring the actual working directory and where the data is coming from. Save the file and make it executable
chmod +x post-receive
Test back on your Local Repo
First set the remote as the upstream master
git push --set-upstream origin master
Make a change to the WordPress theme and commit it
git commit -am "first edit"
Then push it to the remote server
You should see along the lines of
To ssh://email@example.com/home/username/wptheme.git 88003b9..600c2ef master -> master
Check your working directory on the remote server and you should see your WordPress Theme.
Now all changes made locally will be pushed and tracked from local to remote.
Doing the whole webroot
You can use exactly the same technique and apply it to the whole webroot public_html directory and push all changes from local to production. The only thing that you may not want to push is the wp-config.php file especially if you are using simple passwords when on a dev environment like Vagrant VVV, for this you can either just place the legit remote wp-config.php file one directory above the webroot and add the local wp-config.php to the .gitignore file.