In WordPress it’s best just to use GIT on your custom theme or child theme or if you have built your own plugin but otherwise it’s not recommended to use it for plugins form other developers or themes which you plan on updating. This is because those updates will overwrite files in your repository and caused ‘uncommitted changes’ in your repo.
WordPress developers proper don’t really use GIT and WordPress together and for good reason but there are some situations when it’s useful. If you are coming from another PHP framework then you may be used to working in GIT and want to use it in WordPress for theme and plugin development. In this article I’ll show you some ways to use GIT within WordPress.
- What is GIT and why is it useful?
- How do WordPress developers successfully avoid using GIT?
- How to use Git in your WordPress project?
- Pro’s and Cons’ of GIT in WordPress
What is GIT and why is it useful?
GIT is ‘version control’. The basic problem it’s trying to solve is that you can code changes which you later regret making for whatever reason (as this is common). For example, the task you’re working on became insignificant during its execution.
Version Control software like GIT allows you to ‘revert’ to an older version of the code BEFORE you made your changes so you can easily ‘remove’ all that code if you like. Alternatively you could say something like, ‘some of the changes are good but others are bad‘ and GIT has functionality to keep what you want and ‘revert’ what you don’t want.
If you are familiar with Microsoft Word or Google Docs then you have seen their ‘tracked changes‘ feature. GIT is similar in concept but better.
How do WordPress developers successfully avoid using GIT?
My guess is that WordPress users don’t really need this because most of their code is not coded by themselves but instead is installed via plugins and themes and WordPress Core itself. I’m sure the developers who build these do use some kind of Version Control software but probably only for their own plugin/theme/core codebase.
Also, most WordPress users have a backup plugin or service on their host but daily backups so they can ‘revert’ to a previous version albeit with reduced nuance and functionality with their ‘revert’ tool’.
If you need hosting with a backup service checkout the WordPress Host Plan Selector.
How to use Git in your WordPress project?
I have used GIT in WordPress projects to track changes in custom themes and plugins I’ve built for clients. I had a repo set up in bitbucket and would code locally in there before pushing (sometimes pulling if another dev had been working on the same codebase) this code (usually on a new branch) to a ‘development version‘ of the WordPress site I had on the same server as the live site but accessible on a sub-domain.
I used to set up a repo of the WordPress Theme (only) by
- going onto bitbucket and creating a new repo,
- then going to my server via SSH where I had the WordPress site
- and navigating to the theme itself on command line for eg, /pathtosite/wp-content/themes/yourtheme/
- creating a git repo of the theme files and then pushing them to this newly created repo in bitbucket.
- Then on my local computer (my laptop) creating a folder and pulling in this repo.
- At this point I have a copy of the site on my local.
- I would then clone the WordPress site as per the above instructions I mentioned
- and start to work from there with a GIT style workflow.
The general workflow would look something like this,
- create a new branch in my local repo copy
- do my code changes to execute the task,
- then push this up to the repo
- then go to the development version of the site vis SSH and pull in the newly created branch
- switch over to it and test out my changes.
- Make adjustments with the same process just described until I was happy.
- Then to a Pull Request to the repo where I or another developer would peruse and suggest alts before eventually pushing to the live site.
- When I wanted to push my version live I would do a merge (into master or production or whatever your live branch is called) in bitbucket itself using its GIT tools and then
- SSH into the live sites theme directory and pull in the newly merged master branch.
- then I would clear all the caches if the changes had CSS, JS or HTML alterations and
- make sure all was well on the live version before ticking the box on that task.
NOTES:
- You could do the same with a plugin if you were working on a plugin
- If the plugin or theme you are working on has not been installed yet on the WordPress site then you would need to install it first before using the above workflow. Once installed you could do the above easily.
Pro’s and Cons’ of GIT in WordPress
PRO’s
- being that you can track your changes,
- have the ability to work with other devs on the same code base,
- push and pull changes without having to go through the CMS (outside of the initial install of the theme or plugin I mean),
- if someone removes or edits your code then you have a back up in the repo. This is in addition to any other backups you have. Superfluous perhaps but nice to have in my opinion.
CON’s
- The entire site is not working on your local computer so to test changes out I had to go to the development version of the site and test there. An extra step perhaps.
- You could set up the whole site locally but with large sites this is cumbersome and the code base in WordPress was always changing in my situation as admin users added and removes various themes and plugins.
- At some point you aren’t working on the same site that you are testing on so I figured this was unnecessary.