Use Git hooks to automate WordPress theme deployments to a VPS

Deploy a WordPress theme to a VPS with a Git post-receive hook

Deploying with git pull over SSH works, but it is one more manual step after every push, and it needs a working copy on the server that nobody may touch. With a bare repository on the server and a post-receive hook, a git push live main from your computer deploys the theme by itself.

Problem: After every change the developer logs in to the server and runs git pull in the theme folder, and sometimes forgets, or pulls on top of files somebody edited on the server.

Solution: Create a bare repository on the server, add a post-receive hook that checks out the pushed branch into the theme folder, add the server as a remote on your computer, and deploy with git push.

On the server

# A bare repository: only the Git data, no working files
mkdir -p ~/repos/mytheme.git
cd ~/repos/mytheme.git
git init --bare

The hook, ~/repos/mytheme.git/hooks/post-receive:

#!/bin/bash
TARGET="/var/www/html/wp-content/themes/mytheme"
BRANCH="main"

while read oldrev newrev ref; do
    if [ "$ref" = "refs/heads/$BRANCH" ]; then
        echo "Deploying $BRANCH to $TARGET"
        mkdir -p "$TARGET"
        git --work-tree="$TARGET" --git-dir="$HOME/repos/mytheme.git" checkout -f "$BRANCH"
        echo "Done."
    else
        echo "Ref $ref received, not deploying (only $BRANCH is deployed)."
    fi
done
chmod +x ~/repos/mytheme.git/hooks/post-receive

checkout -f writes the files of the pushed commit into the theme folder and overwrites changes made there by hand. The theme folder is not a Git repository itself; the Git data stays in ~/repos, outside the web root, so .git is never downloadable.

The SSH user that receives the push must be allowed to write to the theme folder. Use a deploy user that is in the web server’s group and give the folder group write permissions, rather than running the hook as root.

On your computer

git remote add live ssh://deploy@example.com/~/repos/mytheme.git
git push live main

The output of the hook (Deploying..., Done.) appears in your terminal, prefixed with remote:.

Build steps

If the theme has a build (Sass, JavaScript bundles), either commit the built files, or build on the server in the hook after the checkout:

cd "$TARGET" && npm ci --omit=dev && npm run build

Building on the server needs Node.js there and takes time during which the site may have new PHP files with old CSS. For small sites, committing the built files is the simpler and safer option.

Rolling back

git revert HEAD
git push live main

A revert creates a new commit and deploys it like any other change. Avoid rolling back by force-pushing an older commit: it works, but loses the history of what was deployed.

Managing the server itself over SSH is covered in Linux commands every WordPress developer needs, and the basic Git commands in Git commands every WordPress developer needs.

NOTE: Deploy only your own code (the theme, custom plugins) this way, never wp-config.php, uploads or the whole WordPress installation. Core and third-party plugins are updated by WordPress; a hook that overwrites them with an old version from Git undoes security updates.