DEV Community

Cover image for Set Up a WordPress Staging-to-Production Workflow with Git
Muhammad Usman
Muhammad Usman

Posted on AI-assisted

Set Up a WordPress Staging-to-Production Workflow with Git

We've all done it. You change one line of CSS directly on the live site, hit save, and the homepage turns into modern art.

The fix is boring and it works: write and test your changes on a staging copy of the site, keep your code in Git, and push to production only when everything looks right. This guide sets that up in about 30 minutes. No paid plugins, no deployment platform to learn.

The idea in one minute

Two sites, two branches, one rule.

Site URL Git branch
Staging staging.example.com staging
Production example.com main

The rule: code moves up (local → staging → production). Content moves down (production → staging).

Git tracks your theme (or custom plugin). It does not track your database, your uploads, or wp-config.php. Posts, pages, and settings live in the database, so they never travel through Git. If you remember only one thing from this article, remember that.

What you need

  • A WordPress site on a host that gives you SSH access. Check this before you start, because without SSH this workflow doesn't work.
  • A GitHub account.
  • A recent version of Git on your computer (2.28 or newer, for the git init -b flag below).
  • A subdomain for staging.

I used Hostinger for this setup. If you want to do the same, this link gets you 20% off. It's a referral link, so I may earn a commission if you buy through it. On Hostinger, SSH is available on Premium plans and above, but not on Single plans, so pick accordingly. Everything below works on any host that gives you SSH and Git.

Step 1: Put your theme in a Git repo

We'll version just the theme, since that's where most custom code lives. The same pattern works for a custom plugin.

Name the GitHub repository exactly like your theme folder (here, my-theme). It makes the server steps simpler later.

cd wp-content/themes/my-theme
git init -b main
git add .
git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

Create an empty repository on GitHub (no README, no .gitignore), then connect it and create the staging branch:

git remote add origin git@github.com:YOUR-USERNAME/my-theme.git
git push -u origin main
git checkout -b staging
git push -u origin staging
Enter fullscreen mode Exit fullscreen mode

If your theme has a build step, add node_modules/ to a .gitignore file before your first commit. Nobody wants 40,000 files of node_modules in a repo.

Step 2: Create the staging site

  1. Create the subdomain staging.example.com in your hosting panel.
  2. Install a fresh WordPress there, using the same database table prefix as production. You'll find production's prefix in its wp-config.php (the $table_prefix line, usually wp_). If the prefixes differ, the imported database won't be recognized.
  3. Set up SSL for the subdomain.

Now copy production's plugins and uploads over. The database will say which plugins are active, so they need to exist on staging too. Set two variables first (use your real folders; in hPanel, the Subdomains page shows the folder for your subdomain):

PROD=~/domains/example.com/public_html
STAGING=~/domains/example.com/public_html/staging

mkdir -p $STAGING/wp-content/plugins $STAGING/wp-content/uploads
cp -r $PROD/wp-content/plugins/. $STAGING/wp-content/plugins/
cp -r $PROD/wp-content/uploads/. $STAGING/wp-content/uploads/
Enter fullscreen mode Exit fullscreen mode

If you use a parent theme, copy it from wp-content/themes the same way. Your own theme comes from Git in the next step.

Next, copy the database. If WP-CLI is available on your server (check with wp --info), it takes a few commands. Export from the production folder:

cd $PROD
wp db export ~/prod.sql
Enter fullscreen mode Exit fullscreen mode

Then import into staging and fix the URLs:

cd $STAGING
wp db import ~/prod.sql
wp search-replace 'https://example.com' 'https://staging.example.com' --all-tables --skip-columns=guid
wp option update blog_public 0
Enter fullscreen mode Exit fullscreen mode

The last command tells search engines to ignore the staging site. Nobody needs a duplicate of your site showing up on Google.

After the import, staging uses production's user accounts, so log in with your production username and password.

No WP-CLI? A migration plugin like All-in-One WP Migration can do the same copy.

Step 3: Let your servers pull from GitHub

Each server needs permission to read your private repo. You'll use a deploy key, which is a key pair that can only read one repository.

SSH into your server and generate it:

mkdir -p ~/.ssh && chmod 700 ~/.ssh
ssh-keygen -t ed25519 -C "wp-deploy" -f ~/.ssh/github_deploy -N ""
cat ~/.ssh/github_deploy.pub
Enter fullscreen mode Exit fullscreen mode

Copy the output. In your GitHub repo, go to Settings → Deploy keys → Add deploy key, paste it, and leave "Allow write access" unchecked.

Tell SSH to use that key for GitHub:

cat >> ~/.ssh/config << 'EOF'
Host github.com
  IdentityFile ~/.ssh/github_deploy
  IdentitiesOnly yes
EOF
chmod 600 ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

Test it:

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

The first time, SSH asks whether to trust GitHub's host key. Type yes. Then you should see a message saying you've successfully authenticated. GitHub doesn't give shell access, so the message ends with a note about that. That's normal.

If staging and production live in the same hosting account, one key covers both. Now clone the theme on each site. The commands below use the $PROD and $STAGING variables from Step 2. If you opened a new SSH session since then, set them again first.

Staging (a fresh WordPress doesn't have your theme yet, so this is a plain clone):

cd $STAGING/wp-content/themes
git clone -b staging git@github.com:YOUR-USERNAME/my-theme.git
Enter fullscreen mode Exit fullscreen mode

Production (the live theme folder already exists, so clone next to it, then swap and keep a backup outside the themes folder):

cd $PROD/wp-content/themes
git clone -b main git@github.com:YOUR-USERNAME/my-theme.git my-theme-new
mv my-theme ~/my-theme-backup
mv my-theme-new my-theme
Enter fullscreen mode Exit fullscreen mode

If the clone fails, nothing has changed on your live site. If it works, the swap takes a second. Keep the backup for a while. Future you will say thanks.

Open both sites in a browser and make sure they still look right before you continue.

Step 4: Deploy automatically with GitHub Actions

You could SSH in and run git pull by hand every time. You'd forget within a week. Let GitHub do it.

You need a second key pair, this time so GitHub Actions can log in to your server. Create it on your computer:

ssh-keygen -t ed25519 -C "github-actions" -f ./actions_key -N ""
Enter fullscreen mode Exit fullscreen mode
  • Add the contents of actions_key.pub to your server's authorized keys. On Hostinger, that's hPanel → SSH Access → add an SSH key. Elsewhere, append it to ~/.ssh/authorized_keys.
  • Keep actions_key (the private one) for the next step, then delete both files from your computer.

In your GitHub repo, go to Settings → Secrets and variables → Actions and add these repository secrets:

Secret Value
SSH_HOST Your server's IP or hostname
SSH_PORT Your SSH port (on Hostinger shared hosting it's 65002, and hPanel shows it)
SSH_USER Your SSH username
SSH_KEY The full contents of actions_key

Then add this file to your repo at .github/workflows/deploy.yml. Edit the two THEME_PATH lines to match your server:

name: Deploy theme

on:
  push:
    branches: [staging, main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Pull latest code on the server
        env:
          SSH_KEY: ${{ secrets.SSH_KEY }}
          SSH_HOST: ${{ secrets.SSH_HOST }}
          SSH_PORT: ${{ secrets.SSH_PORT }}
          SSH_USER: ${{ secrets.SSH_USER }}
          BRANCH: ${{ github.ref_name }}
        run: |
          if [ "$BRANCH" = "main" ]; then
            THEME_PATH="~/domains/example.com/public_html/wp-content/themes/my-theme"
          else
            THEME_PATH="~/domains/example.com/public_html/staging/wp-content/themes/my-theme"
          fi

          mkdir -p ~/.ssh
          echo "$SSH_KEY" > ~/.ssh/deploy_key
          chmod 600 ~/.ssh/deploy_key
          ssh-keyscan -p "$SSH_PORT" "$SSH_HOST" >> ~/.ssh/known_hosts
          ssh -i ~/.ssh/deploy_key -p "$SSH_PORT" "$SSH_USER@$SSH_HOST" \
            "cd $THEME_PATH && git pull --ff-only origin $BRANCH"
Enter fullscreen mode Exit fullscreen mode

Commit this file on staging and push. Then merge it into main and push again. From now on, pushing to staging updates the staging site, and pushing to main updates production. You can watch each run in the Actions tab of your repo.

If your staging and production sites are on different hosting accounts, you'll need separate host, user, and key values for each. Add them as extra secrets and pick them in the if block the same way.

Your daily workflow

# 1. Start a branch for your change
git checkout staging
git pull
git checkout -b fix/header-spacing

# 2. Work, test locally, commit
git add .
git commit -m "Fix header spacing on mobile"

# 3. Merge into staging and push (auto-deploys to staging)
git checkout staging
git merge fix/header-spacing
git push

# 4. Check staging.example.com in a browser. Click around. Be suspicious.

# 5. Happy? Merge into main and push (auto-deploys to production)
git checkout main
git pull
git merge staging
git push
Enter fullscreen mode Exit fullscreen mode

Step 4 is the whole point of this setup, so don't skip it.

Rolling back

If something slips through, undo it with Git instead of panicking in a file manager. Find the bad commit and revert it:

git checkout main
git log --oneline
git revert COMMIT-HASH
git push
Enter fullscreen mode Exit fullscreen mode

That creates a new commit that undoes the bad one, and the deploy runs again. Your history stays intact. If the bad commit is a merge commit, use git revert -m 1 COMMIT-HASH instead.

Then bring the fix back to staging so the two branches agree:

git checkout staging
git merge main
git push
Enter fullscreen mode Exit fullscreen mode

Things that will bite you

  • Don't edit theme files on the server. The next git pull can fail because the server copy has changes Git doesn't know about. Make all edits locally.
  • Database changes don't deploy. New pages, widget settings, and page-builder layouts live in the database. Build them on staging if you want, but you'll have to redo them on production, or sync the database deliberately.
  • Refresh staging from production now and then. Repeat the database part of Step 2 so staging doesn't drift away from the real site. This overwrites the staging database, so don't do it in the middle of testing something.
  • Keep emails in check on staging. A staging copy with real customer data can send real emails. Install an SMTP-blocking plugin there, or disable outgoing mail.

Wrapping up

Once this is running, deploying becomes a git push and a short wait. Your live site stays calm, and the homepage stays a homepage.

Need a host for this setup? Get 20% off Hostinger with my referral link.

Disclosure: this is a referral link. You get 20% off when you buy through it, and I may earn a commission.

Top comments (0)