GitHub for WordPress Site Deployment
If you haven’t used Git before, it can feel overwhelming to implement and understand how you might use GitHub for WordPress. I’m confident that you will have a decent understanding of how to implement and operate Git in a WordPress project by the end of this article.
Let’s start by thinking of Git as a place to store code online. It also provides a way of adding to the code, accessing the code, and so on. It also maintains a thorough history of every change/commit made to the code base. Git is an extremely powerful development tool. It can be used to create parallel branches of code for testing new features. This allows experimental work without impacting the functioning version. Code changes can be implemented, tested, then reverted or thrown out if it isn’t working out. It allows code to exist in multiple locations while keeping all of them in sync. This is merely the tip of the iceberg. The rest requires a much deeper dive.
Most developers I know use Git from the command-line. If that is too foreign to you, there are some fantastic GUI systems available that can help achieve the goal. I’ll focus on the command-line here, but all the concepts apply the same to a GUI version.
Typically WordPress code lives two places: on the local development machine and the web server. Code and files can be updated via FTP or some other method. Using GitHub for WordPress, the WordPress code will exist in 3 places. There will be a local copy for development, a copy on your web server, and the master version in GitHub.
Go to GitHub and get an account if you don’t have one. It’s 100% free, by the way. They even let you have private repositories at no cost. Thank you, Microsoft.
Create a repository. Give it a unique name, and a description if you can. Make it private too. Once you complete this step, you will be provided a URL link to your repository. Copy this, we will use it in the next step.
Turn the wp-content folder into a git enabled directory and add the remote repository address. Essentially, this step is informing our wp-content folder where to send data for storage and versioning. This is fairly simple if you have SSH access to your server. On your web server, use the following commands to get it done:
- cd ~/var/www/<your site>/wp-content
- sudo git init
- sudo git remote add origin <the link you copied in step two>
Now we need to add the wp-content folder to our Git repository and upload the data. While we are still in the wp-content folder root, do the following:
- sudo git add .
- sudo git commit -m ‘init commit’
- sudo git push origin master
That’s it! Now you can access your code in the repository from anywhere. To copy the code locally, navigate to the root of your local WordPress project and remove the wp-content folder, then pull down the code. Use the following to do so:
- cd path/to/your/local/env/root
- rm -rf wp-content
- git clone <link to your repo> wp-content