Git is a version control system widely used by teams and companies to manage repositories and collaborate on code. One of Git’s most useful features is its hook system, which can run commands automatically after specific Git actions. With the right hook, you can use Git itself as the deployment mechanism for a WordPress site.
Here is a simple workflow for automatically deploying WordPress to a server after repository updates.
1. Install an OpenSSH server
Many Linux servers already include an SSH server and client, so this step can often be skipped. If they are not installed, add them first:
sudo yum install openssh-server openssh-client
2. Create your public and private keys
Run the following command in the current user’s home directory and follow the prompts to create an SSH key pair:
ssh-keygen -t rsa
By default this creates a 2048-bit key. If you want a stronger key, generate a 4096-bit key instead:
ssh-keygen -t rsa -b 4096
By default the keys are stored inside ~/.ssh, for example:
id_rsa
id_rsa.pub
known_hosts
Keep the private key safe and never expose it. Add the public key to the server so the deployment user can authenticate with SSH.
3. Set up a Git server with Gogs
Gogs is one of the easiest self-hosted Git servers to install and configure. In many cases the whole installation can be done with a single command sequence:
sudo rpm --import https://rpm.packager.io/key
echo "[gogs]
name=Repository for pkgr/gogs application.
baseurl=https://rpm.packager.io/gh/pkgr/gogs/centos6/pkgr
enabled=1" | sudo tee /etc/yum.repos.d/gogs.repo
sudo yum install gogs
After that, visit http://your-server-ip:3000 to complete the web-based setup and create your repository.
4. Automatically update the site when the repository changes
Once Git is in place, use the repository’s post-receive hook to check out the latest code directly into the site directory. Edit hooks/post-receive in your bare repository and adjust the deployment path for your server.
#!/bin/sh
site=/home/wwwroot/example.com
export GIT_WORK_TREE=$site
git checkout -f
After those steps are complete, the deployment workflow is ready. You simply commit and push your updates, and the server’s WordPress code is refreshed automatically. For teams that already use Git, this can be a very convenient lightweight deployment strategy.
