Deploy with Git

If you’re still deploying and updating sites with FTP or by generally pushing files around, here’s an easier way—deploy with Git!

The basic principle is to use git pull origin master on your production server to pull down the latest version and it helps to maintain multiple branches (at least master for production and dev for development) or even use Feature Branching for more control.

Workflow

Your development workflow could be:

  1. git pull origin dev && git checkout dev to get latest development branch and switch to it
  2. Do your work
  3. git add whatever.php to stage changed files
  4. git commit -m 'Did something' to commit your changes
  5. git push origin dev to push changes to the server
  6. git checkout master && git merge dev to switch to master and merge your work

Then you just need to SSH to your production server, cd into the site and git pull origin master to handle the process of actually updating the files.

Rolling-back

Git manages your versioning, so you can use it to roll-back. Before you deploy, get the hash for the current version (e.g. 694d0ea280) so that if you need to roll-back you can just run git checkout 694d0ea280.

Knowing you can safely and efficiently roll-back is a huge advantage, especially when you know how exactly how to do it before you ever need to!

WordPress Plugins/Themes

Deploying a WordPress Plugin or Theme with Git is a little more tricky, because if someone visits the site when the folder name has changed (while making the switch between the static folder and the git repo) the plugin/theme will be deactivated, potentially losing your configuration and giving as visitor a bad experience.

On a corporate production site, for example, reverting to the Twenty Fourteen theme just isn’t acceptable!

The trick is to make the switch in one command, so if the theme folder is called “my-theme” and the repo is called “my-theme-repo”, clone the repo in WordPress’s themes directory, then run mv my-theme my-theme-old && mv my-theme-repo my-theme so the old theme is renamed to “my-theme-old” and the repository takes its place.

Now you can deploy by cding into the theme directory and following the workflow above.

Leave a Reply

Your email address will not be published. Required fields are marked *