Using git with WordPress on SiteGround

I use SiteGround for most of my personal websites. I love it because I can create unlimited sites without any additional cost.

I prefer to use git to deploy code rather than SFTP. I make my changes locally, and when I’m happy with it I push them with git push production master.

SiteGround has git support built-in, but it works a bit differently than other hosts with git push. This article by 9seeds provided a lot of guidance here.

Add a .gitignore file

By default, SiteGround will version control the entire wp-content directory: your themes, plugins, and uploads. If you prefer to only include certain themes and plugins (like I do), you’ll first want to add a .gitignore file before creating the git repository in SiteGround.

I had migrated a site to SiteGround, then clicked “Create git repository”. The repo was massive because it included 2GB of uploads. Adding the .gitignore afterwards and deleting the uploads directory wouldn’t help because the uploads are still part of the history of the repo. I fixed it by deleting the repo, adding the .gitignore file, and creating a new repo.

Here’s my sample .gitignore file. Edit it to include any themes and plugins you’d like to version control, then upload to the top level of your WordPress site.

Create the repo on SiteGround

Log into SiteGround and open cPanel. Click on the “SG-Git” tool (use the search on the left to find it).

At the bottom you’ll see a list of domains that are not currently version controlled. Click the green “Create git repository” button next to the site you want.

You’ll then see a popup window containing a “git clone” command and an ssh key. You’ll only need the git clone command for what we’re doing.

Copy the git clone command and run it locally wherever you want the site to be. For instance, I have a /wpdev directory on my computer where I have most of my sites, so within that I run the git clone command. It will create a new directory using your domain name (ex: /wpdev/yoursite.com). You can rename it if you like: mv yoursite.com yoursite

Integrating GitHub

Your new local repository will have a remote named origin pointing to SiteGround. I prefer having origin point to GitHub and production point to the live site. To change this:

  1. Rename the remote: git remote rename origin production
  2. Create a private repo on GitHub. Click “Clone or download” and copy the URL given (screenshot).
  3. Add the remote origin: git remote add origin {url here}

Private GitHub repos are only available for paid accounts ($7/month). Alternatively, you could use the same approach with BitBucket which offers unlimited private repos for free.

Making code changes

When you’re ready to push them to GitHub, run: git push origin master. When you’re ready to deploy your code changes to your website at SiteGround, run: git push production master. I also like to use WP Migrate DB Pro for pushing and pulling the database.

That’s it! It’s pretty simple to setup and a lot simpler than SFTP for deploying code changes. Let me know if you have any questions or additional SiteGround tips.

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Reader Interactions

Comments

  1. Bill, Thanks so much for this. One question. Would this work if I used Siteground to create an additional environment? So for example, pushing from various developers locals to a development environment to test, and then to the live environment. How would the push from dev to live work?

    • I haven’t tried it but yes, I think that would work just fine. You would create two installs – dev.yoursite.com = the development remote, and http://www.yoursite.com = the production remote.

      As you’re developing, push changes to origin (GitHub) and development for review by the team. Once it’s all approved, push code to production.

    • I am also trying to figure out how this works, specifically with Siteground’s staging site feature. Through cPanel you can click a button to make the staging version live. I’m not sure exactly what happens behind the scenes there how that would work with a git repo for the staging site. Would I need two repos? One for staging site, one for live site?

      • I haven’t used the staging feature in SiteGround, but I would create a single repo and have two remotes (staging and production).

        To send code to the staging environment, run git push staging master

        To push code changes to the development environment, run git push production master.

        You might not need to run the production code push if SiteGround’s “Staging to Production” feature copies all code over.

  2. Very good tutorial, too bad is only applicable for GoGeek accounts as the other two service tiers do not include git functionality.

  3. Bill, thank you very much, this is a very useful and much-needed article.
    May I ask, one tiny detail, within this workflow how you manage the local installation? Assuming that we’re using Desktopserver let’s say, do we first make a local WP installation into /wpdev/yourwebsite.com/ ?
    Due to .gitignore configuration, we will only clone two folders: the chosen theme and plugin folder; so I’m guessing that we need to solve the core WP installation somehow somewhere, we can’t clone core WP files from Siteground, as the database or .htaccess are all different (maybe we are solving DB with WP Migrate DB Pro) yet WP versions might be different on the production Siteground and in your local installation.

    • Here’s a description of my development workflow.

      Yes, you would create a new WP installation locally. We’re only keeping our custom theme and CF plugin version controlled, which simplifies the management of multiple installs. You could have different plugins installed on both, different database credentials in wp-config.php… because none of that is version controlled.

      I do use WP Migrate DB Pro to push/pull the database, but it works the same with any other approach (db dump from phpMyAdmin, wp db export with wp cli…).

  4. Bill,

    I’ve have been trying this resolve this exact same issue. I’m going to give your solution a go tomorrow, thanks again.

  5. Hi Bill,
    Thanks for your article – this almost works for me. Perhaps its the difference in accounts, but if I go to create a repo using their SG GIT Tool (on my SiteGround Cloud VPS account) the pop-up window you describe containing GIT Clone & an SSH Key is non existent – it just straight away starts creating the repo.
    Out of interest, I take it you just dropped your .gitignore file in the WP site’s root directory?
    I have a support ticket in with SG now, & with Bitbucket to try & get my process all sorted.
    If I get anything good, I’ll post it here.
    I’m only wanting to track custom plugin & theme code too – nothing else.

    • Correct, I dropped the .gitignore file in the WP site’s root before using their SG Git tool.

      Please share whatever you find out from their support.

      Thanks

Leave A Reply