WordPress Git deployments for Kinsta via SSH

I’ve interacted with a number of different WordPress hosts over the last few years, but Kinsta has been the best by far in terms of support, performance, and security. Since I’ve been recommending Kinsta for every new client, I’ve decided I needed an easy & fast process to develop new features on my local server and deploy them to Kinsta.

I’ve done some research and found few different options, including plugins and services. However, I decided to go with Git, because, you know! I want to be a good developer.

So without further introduction, here is the process:

On Server

  • Connect via SSH
  • Create a bare git repo under the private folder. This will be the deployment repo.
git init --bare /private/{sitename}.git
  • Access the public folder and initialize a new git repo.
cd public
git init
  • Access the private repo hooks folder and create a post-receive hook.
cd /www/{sitefolder_xxxxxxx}/private/{sitename}.git/hooks
nano post-receive
  • Add the following code inside the post-receive file, make the necessary changes, save and quit nano ( CTRL+X -> Y -> Enter ).

while read oldrev newrev ref
        # only checking out the master (or whatever branch you would like to deploy)
        if [[ $ref = refs/heads/$BRANCH ]];
                echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
                git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f
                echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
  • Assign execute permissions to post-receive file
chmod +x post-receive

On your local computer

  • Access the WordPress installation folder you’ll be working on and initialize a new repo.
cd /wordpress/path
git init
  • Add .gitignore file and make sure to ignore everything except the files/folders you’ll be working on. You can use this template:
# ignore everything in the root except the "wp-content" directory.

# ignore everything in the "wp-content" directory, except:
# "plugins", "themes" directory

# Ignore everything in the "plugins" directory, except the plugins you
# specify (see the commented-out example.)
# !wp-content/plugins/example-plugin/

# Ignore everything in the "themes" directory, except the themes you
# specify (see the commented-out example.)
  • Add a remote
git remote add production ssh://{username}@{ip}:{port}/www/{sitefolder_xxxxxxx}/private/{sitename}.git
  • Make changes, add, commit and push. However, on the first time make sure to use `git push -u production master` instead of `git push` to specify which branch to use.
git add .
git commit -m "message"
git push

That’s it, your life as a WordPress developer just got a bit easier, you’re free from copying and pasting files over FTP.

Leave a comment