There is a moment most web developers remember: you upload a "small fix" over FTP, overwrite the wrong file, and the live site breaks. You don't know exactly what changed, and there is no clean way back.
Deploying with Git fixes that. Every change is recorded, every release is a known version, and rolling back is a command instead of a panic. This guide covers the main ways to deploy a website with Git, from the simplest setups on shared hosting to automated pipelines.
Why deploy with Git?
- History: every change has an author, a date and a message.
- Repeatability: the server gets exactly what is in the repository, not whatever happened to be on someone's laptop.
- Rollbacks: going back to the last good version is quick.
- Teamwork: multiple people can work on branches and merge changes safely.
- Fewer accidents: no more partially uploaded folders or forgotten files.
If you aren't using Git for your site at all yet, start there. Even without automatic deployment, having your code in a repository is a big step up.
What should (and shouldn't) be in the repository
Before deploying, get your repository right:
Commit:
- Your theme, plugin or application code
- Configuration templates (for example config.example.php)
- Build scripts and dependency manifests such as composer.json and package.json
Don't commit:
- Passwords, API keys and real configuration files with secrets
- Uploaded media (wp-content/uploads)
- Dependency folders like vendor/ and node_modules/, which can be rebuilt
- Cache folders and logs
Use a .gitignore file to keep these out. For a WordPress site, a common approach is to track only your custom theme and custom plugins, not WordPress core or the uploads folder.
Method 1: Pull on the server
The simplest approach. You clone the repository on the server once, then pull updates when you want to deploy.
ssh user@yourserver
cd ~/public_html
git pull origin main
Pros: simple, easy to understand.
Cons: the .git folder lives inside your web root, so you must block web access to it, and someone has to log in to deploy.
If you use this method, make sure the .git directory can't be downloaded. On Apache you can add this to .htaccess:
RedirectMatch 404 /\.git
Exposed .git folders are a common way source code and secrets leak.
Method 2: Push to deploy with a bare repository
A more polished setup. You create a "bare" repository on the server (one with no working files) and a hook that checks out the code into your web root whenever you push.
On the server:
mkdir -p ~/repos/site.git
cd ~/repos/site.git
git init --bare
Create ~/repos/site.git/hooks/post-receive:
#!/bin/bash
TARGET="$HOME/public_html"
GIT_DIR="$HOME/repos/site.git"
BRANCH="main"
while read oldrev newrev ref; do
if [ "$ref" = "refs/heads/$BRANCH" ]; then
git --work-tree="$TARGET" --git-dir="$GIT_DIR" checkout -f "$BRANCH"
echo "Deployed $BRANCH to $TARGET"
fi
done
Make it executable with chmod +x hooks/post-receive. Then, on your computer:
git remote add production ssh://user@yourserver/~/repos/site.git
git push production main
Every push to main now updates the live site. The repository data stays outside the web root, so there is no .git folder to protect.
Method 3: cPanel Git Version Control
cPanel includes a built-in Git Version Control tool. You can create or clone a repository from the interface and deploy it using a .cpanel.yml file in the root of your repository that lists the deployment tasks. A minimal example:
---
deployment:
tasks:
- export DEPLOYPATH=/home/username/public_html/
- /bin/cp -R wp-content/themes/mytheme $DEPLOYPATH/wp-content/themes/
Replace username and the paths with your own. You then push changes to the cPanel-hosted repository (or pull from a remote such as GitHub) and click "Deploy HEAD Commit," or let the push trigger deployment. If your hosting plan includes SSH access, this is a tidy way to get Git deployment without writing hooks by hand. In cPanel it usually appears under the Files section, if your host has it enabled. Host Whimsy customers can find help with cPanel and account access in our knowledge base.
Method 4: Automated deployment with CI/CD
For teams, a continuous integration service builds and deploys code automatically when changes are merged. With GitHub Actions, for example, a workflow can:
- Check out the code.
- Install dependencies and run a build step (Composer, npm).
- Run tests or linters.
- Upload the result to the server with
rsyncover SSH.
Store the SSH private key as an encrypted repository secret, never in the code. Use a dedicated deploy key or user with access only to what deployment needs.
Pros: tests run before anything goes live, builds happen in a clean environment, and nobody needs server access to deploy. Cons: more setup, and you need to understand the pipeline when it fails.
Handling builds and dependencies
If your project needs a build step (compiled CSS, bundled JavaScript, Composer packages), decide where it happens:
- On the server, in your deploy hook: requires Node or Composer on the server and adds load during deploys.
- In CI, then upload the built files: keeps the server lean and is usually the better option.
Either way, avoid committing node_modules or vendor unless you have a strong reason.
Configuration and secrets
Keep environment-specific settings out of Git. Common approaches:
- A
.envfile on the server, listed in.gitignore - Server environment variables
- A
wp-config.phpthat lives only on the server
Your deploy process should never overwrite these files.
Rolling back
With Git, rolling back means deploying an earlier commit. You can revert the bad commit and push again:
git revert <bad-commit-hash>
git push production main
More advanced setups deploy each release into its own timestamped folder and switch a current symlink, so a rollback is just pointing the symlink back. If you do that with PHP, remember to reload PHP-FPM or reset OPcache after switching, so cached code doesn't linger.
Remember that Git rolls back code, not your database. Always keep regular backups as well.
Good habits for safe deploys
- Deploy from one branch (usually
main) and protect it. - Test changes on a staging site first.
- Deploy during quieter hours for big changes.
- Write clear commit messages so you know what went live.
- Keep a short checklist: backup, deploy, clear caches, smoke-test key pages.
Wrapping up
Deploying a website with Git turns releases from a nervous file-upload ritual into a predictable process. Start simple, with a pull on the server or cPanel's Git Version Control, then move to push-to-deploy hooks or a CI pipeline as your project grows. Keep secrets and uploads out of the repository, protect your .git folder, and you will always know exactly what is running on your live site.