Use Git to update your customized Wordpress Themes safely
Using Git to manage Theme Versions in Wordpress is a little bit geeky, but can help you a lot if you customize off the shelf (premium or free) themes. As good as these themes are to get a wordpress site up and running quickly, they also need some tweaking. Preserving these tweaks is not as easy. How can you keep track of sometimes multiple files you modified? Why not use a version control system that helps merging code bases. It let's you test out your tweaks, giving you a quick way back if you break something and it's the best tool to merge your changes into an updated version of the theme. Git is such a solution and it's fairly easy to use, once you pass the first hurdle of grasping the concept.
Git (and Github, the web service where you can host your git repositories) are subject in books and many articles on the web, so I leave that part out. In this little step my step I assume that you have Git installed on the web-server you host your Wordpress installation and have command line (ssh) access.
Setting up a theme with Git
- Install new theme with Wordpress Install as usually with the management tools provided by Wordpress
- Create a new repository on the web-server
SSH into the /wp-content/themes/ and create a new repository
git init // create new repository git add . // add all files in folder git commit -m "original version" // commit with comment
- Get ready for future updates
git branch ready4update // add new branch
This will create a branch in which we will then copy the updated theme files when we need to update - Make your modification to the themeNow you can make your changes to the theme as you like - you will continue to make changes to the master branch. You need to commit your changes to register them with git and I highly advise to use the workflow approach described in ProGit to make your work even more efficient.
To commit your changes just enter
git add . // add all files in folder git commit -m "Insert your comment here" // commit with comment
Update the theme
Now comes the neat part. You have an updated theme and you need to merge it with your customized one. To do this we need to activate the branch we prepared in step 4.
- Install new theme files
git checkout ready4update // go to branch
Now copy the updated theme files into the folder and overwrite everything and commit the changes to the ready4update branch.
git add . // add all files in folder git commit -m "updated theme" // commit with comment
- Get Master branch ready
We go back to our master branch and make a temporary branch to have a save update.
git checkout master // go to master branch git checkout -b tempupdate // create new temporary branch from master and change to it
- Merge the update into the temporary branch
And now we merge the two branches
git merge ready4update // merge with tempupdate
The trick here is that the master and ready4update branch have the original theme as common ancestor. Git can merge this way the changes made by the author of the theme and yours - and if you have conflicts git will help you resolve them.
- Test the website Since we have created a tempupdate branch we are working in, even if something brakes badly you can still go back to your previous stable master branch (git checkout master).
- Merge into master and get ready for next update
If the test went well and your wordpress site works well you should get ready for the next update
git checkout master // go to master branch git merge tempupdate // merge the branch into master git branch -d tempupdate // delete temporary branch git branch -d ready4update // delete ready4update branch git branch ready4update // create new ready4update branch
Now you are ready to update your theme or make additional changes to it. Believe me - it looks more complicated then it actually is, so go ahead and try it.