Using Git instead of FTP for WordPress deployments

The default way most developers deploy WordPress websites from their local development machines to the either shared or dedicated hosting production environment is mostly via FTP.

The fact is, FTP is very simple to use. It’s the default method especially for websites that are hosted under shared hosting arrangement. There are several great FTP clients out there such as Filezilla, Coreftp, WinSCP, Transmit (Mac OS X), FireFTP (All Platforms with Firefox), Cyberduck (Mac OS X) that you can use to push files to remote hosting server. But if you have access to dedicated server such as a VPS, then you want to consider using git instead of FTP for pushing changes to production.

Git is a software version control tool which you can use to keep track of changes of your code. It’s a commandline tool mostly although there are several GUI versions out there. It’s also intergrated in most modern IDEs such as Jetbrains suit of IDEs, Visual studio code etc. What’s even great about git is that it allows collaborative software development making it easy for you to work as a team on a project. You can even intergrate it with CI/CD pipelines which automate testing and deployment of code. For this purpose, git makes it super easy and very convenient to push changes from your local machine to remote host.

So if your WordPress website is running on a dedicated Virtual Private Server, I highly recommend you switch from using FTP to Git for deploying changes. I do that for high traffic WordPress websites like https://www.dignited.com which I manage. So how do you do it?

First I have an account with bitbucket where I create the website remote git repository. This is where the latest website code lives and I can invite team members into the repository so they can see changes I made. Bitbucket has generous free package offer for private repositories although Github recently countered that.

Before you do anything be sure to add public SSH keys of your development PC and the production server to Bitbucket or Github. SSH keys ensure you don’t have to enter login password each them you make deployments. On Linux/Mac OS, public SSH key is in the ~/.ssh/id_rsa.pub file.

You can initiate the website repo on your local machine or you can first create the remote repo on Bitbucket, add the remote repo to your local machine before pushing the changes.

git init
git commit -m "first commit"
git remote add origin git@bitbucket.org:username/website-name.git
git push -u origin master

…or push an existing repository at bitbucket.org from the command line

git remote add origin git@bitbucket.org:username/website-name.git
git push -u origin master

Then I use .gitignore file to ignore the wp-content/uploads/ folder which WordPress uses to store website images. For a high traffic website like Dignited, that folder alone is about 6 GB so far. This leaves me with WordPress core files, the website theme and plugin portions of the code to keep track and deploy which comes to less than 50 MB.

When I make changes to the website code and test locally, I simply git push to remote repository. Then on the production server, simply have to git pull the changes from shared remote Bitbucket repo. If the changes break the website, I can easily roll them back within seconds without anyone noticing.

Suppose $COMMIT was the last commit id before you performed git pull. What you need to undo the last pull is

git reset --hard $COMMIT

This is the magic of Git over FTP. If you pushed your changes using FTP, you would have to manually figure out the changes you made and carefully edit the code to last working state. Also Git deploys only pieces your website code which have changed, not the entire folder. This save you on bandwidth/data and even time taken to deploy changes.

So are you going to switch over to Git? If the website is on shared hosting, this is not an option. Git requires terminal/shell access to the server which shared hosting providers don’t provide. But if the website is on a VPS, then by all means the choice is up to.

Image: Pixabay

Leave a Reply

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