keepcalculator

06 Numbers and Powers

Automate WordPress Deployments with Git and Staging

Card No. 06·71855 · Updated · Standard formula, shown below

This guide walks you through setting up an automated WordPress deployment workflow using Git and a staging environment. You’ll learn how to push changes from your local development site to a staging server, test them, and then deploy to production—all with a few Git commands. This eliminates manual FTP uploads, reduces human error, and makes rollbacks as simple as reverting a commit.

What You’ll Need

  • A local WordPress installation (e.g., using Local, XAMPP, or a remote dev server)
  • A staging server (subdomain like staging.example.com) and a production server (your live site)
  • SSH access to both servers (shared hosting may require a workaround)
  • Git installed on your local machine and on both servers
  • Basic comfort with the command line

Step-by-Step

  1. Set up Git repositories – On your local machine, navigate to your WordPress root folder and run git init. Create a .gitignore file to exclude wp-config.php (which contains database credentials), the /wp-content/uploads/ directory, and any local environment-specific files. Commit your entire codebase with git add . and git commit -m "Initial commit".
  2. Create a bare repository on the staging server – SSH into your staging server and create a bare Git repository, e.g., mkdir /var/repos/staging.git && cd /var/repos/staging.git && git init --bare. This repository will receive your pushes.
  3. Add a post-receive hook on staging – In the bare repository’s hooks directory, create a file named post-receive (no extension). Make it executable with chmod +x hooks/post-receive. The hook should check out the latest code into your staging web root. A typical hook looks like this:
    #!/bin/bash
    TARGET="/var/www/staging"
    GIT_WORK_TREE=$TARGET git checkout -f
    

    Replace /var/www/staging with your actual staging web root.

  4. Add the staging server as a remote – Back on your local machine, add the staging server as a Git remote: git remote add staging ssh://user@staging-server.com/var/repos/staging.git. Push your code with git push staging master (or your main branch name). The post-receive hook will automatically update the staging files.
  5. Configure the staging database – Create a database for staging (via cPanel or command line). Copy your production database to staging (or import a fresh copy). Update the staging wp-config.php with the new database credentials and set WP_HOME and WP_SITEURL to the staging URL. Also, ensure you have a plugin like “Better Search Replace” or use WP-CLI to update the site URL in the database (e.g., wp search-replace 'https://example.com' 'https://staging.example.com').
  6. Set up the production server similarly – Repeat steps 2–4 for your production server, but use a separate bare repository (e.g., prod.git) and a post-receive hook that deploys to your production web root. Add it as a remote: git remote add production ssh://user@prod-server.com/var/repos/prod.git.
  7. Create a deployment workflow – Now you can develop locally, commit changes, and push to staging. Test everything on staging. When you’re satisfied, push the same commit to production: git push production master. The production hook will update the live files.
  8. Handle database changes – Git only tracks code, not the database. For schema changes, use a migration plugin (like WP Migrate DB Pro) or a tool like WP-CLI to export/import the database. Alternatively, use a version-controlled SQL file and apply it manually on both environments.
  9. Automate uploads syncing – Since uploads are excluded from Git, you need to sync them separately. Use rsync to copy uploads from production to staging (and optionally back). For example: rsync -avz user@prod:/var/www/html/wp-content/uploads/ /var/www/staging/wp-content/uploads/.
  10. Test the full flow – Make a small change (e.g., a comment in a theme file), commit, push to staging, verify it appears. Then push to production and verify. This confirms your hooks and remotes are correct.

Common Mistakes

  • Forgetting to exclude wp-config.php – If you commit it, you risk leaking database credentials and breaking the staging site because the local database settings won’t match.
  • Not updating the site URL in the database – After copying the database, the site will still point to the production URL, causing redirects and broken assets.
  • Pushing to production before testing on staging – Skipping the staging step defeats the purpose and can introduce bugs to live users.
  • Using the same branch for everything – It’s better to use a develop branch for staging and main for production. Push to staging from develop, then merge into main and push to production.
  • Ignoring file permissions – The web server needs write access to certain directories (like wp-content/uploads). Ensure your hooks run with the correct user and that permissions are set properly.

Tips

  • Use SSH keys instead of passwords for Git to avoid repeated authentication prompts.
  • Set up a cron job on the staging server to periodically sync uploads from production so your staging site always has the latest media.
  • Use a plugin like “Advanced Custom Fields” with JSON sync to keep field groups in version control.
  • Consider using a deployment tool like DeployHQ or GitHub Actions if you prefer a GUI or want to automate the process further (e.g., run tests before deploying).
  • Tag your releases with Git tags (e.g., v1.2.3) to make rollbacks easier.
  • Keep a backup of the production database before each deployment, just in case.

Troubleshooting

  • Push succeeds but files don’t update – Check the post-receive hook’s permissions (must be executable) and the target path. Run the hook manually with bash hooks/post-receive to see errors.
  • Site shows “Error establishing a database connection” – The database credentials in wp-config.php are wrong, or the database hasn’t been imported. Double-check the host, database name, user, and password.
  • Site redirects to production URL – The database still has the old URL. Run wp search-replace again, and also check wp_options for siteurl and home.
  • Git push is slow or times out – The repository may be large due to uploaded files being tracked. Ensure your .gitignore is correct and remove any large files from Git history using git filter-branch or BFG Repo-Cleaner.
  • Post-receive hook runs but files are owned by wrong user – The web server may not be able to read the files. Set the correct ownership (e.g., chown -R www-data:www-data /var/www/staging) and permissions.
  • Cannot push to remote because of “non-fast-forward” – Your local branch is behind the remote. Pull the changes first: git pull staging master (or use --force with caution).

Frequently Asked Questions

Can I use this with shared hosting?

Yes, but you need SSH access. Many shared hosts provide SSH, but if not, you may need to use a deployment tool that uses SFTP, or upgrade to a VPS.

What about database changes?

Git doesn’t track databases. You’ll need to export/import the database manually or use a migration plugin. For automated schema changes, consider using a tool like WP-CLI or a migration script.

How do I rollback a bad deployment?

Simply revert to the previous commit locally and push again: git revert HEAD or git reset --hard HEAD~1 then push to production. The files will be restored to the previous state.

Should I use branches for staging and production?

Yes, it’s a good practice. Use a develop branch for staging and main for production. Merge changes into main only after testing on staging.

How do I handle WordPress core updates?

You can update WordPress core through the admin dashboard, but this will modify files outside Git. To keep everything in Git, update core locally, commit the changes, and push. Alternatively, exclude core from Git and update on each server separately.

Is it safe to exclude wp-config.php from Git?

Yes, it’s the recommended practice. Each environment should have its own wp-config.php with the correct database credentials and URLs. You can use a sample file like wp-config-sample.php in Git to document required constants.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →