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
- Set up Git repositories – On your local machine, navigate to your WordPress root folder and run
git init. Create a.gitignorefile to excludewp-config.php(which contains database credentials), the/wp-content/uploads/directory, and any local environment-specific files. Commit your entire codebase withgit add .andgit commit -m "Initial commit". - 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. - Add a post-receive hook on staging – In the bare repository’s
hooksdirectory, create a file namedpost-receive(no extension). Make it executable withchmod +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 -fReplace
/var/www/stagingwith your actual staging web root. - 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 withgit push staging master(or your main branch name). The post-receive hook will automatically update the staging files. - 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.phpwith the new database credentials and setWP_HOMEandWP_SITEURLto 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'). - 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. - 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. - 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.
- 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/. - 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
developbranch for staging andmainfor production. Push to staging fromdevelop, then merge intomainand 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-receiveto see errors. - Site shows “Error establishing a database connection” – The database credentials in
wp-config.phpare 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-replaceagain, and also checkwp_optionsforsiteurlandhome. - Git push is slow or times out – The repository may be large due to uploaded files being tracked. Ensure your
.gitignoreis correct and remove any large files from Git history usinggit filter-branchor 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--forcewith 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
- Samsung Unpacked August 5: 5 Devices Teased?
- Google Workspace Accounts Created Bypassing?
- Recover Deleted Screenshots on Mac: Full Guide
- Go vs Node.js: Cloudflare API Request Data
- ROG Phone 3 Certified Specs Leak: What’s Confirmed
- Clear Cache on a Philips Smart TV: Right Menu