In a previous article, we set up a minimalist Git server using only SSH: a dedicated user, bare repositories, restricted keys, and zero external dependencies.
Now let’s take it to the next level of usefulness: automated deployment every time you push.
With a single hook, every git push will directly update your application in production. No GitHub Actions, no GitLab CI, no endless Docker Compose setups, no 400-line YAML files. Just Bash, Git, and your VPS.
The post-receive hook: What it is and why it’s useful
Git has several points where you can attach custom scripts (the well-known hooks). The one we care about today is post-receive, which runs on the server right after the push has been successfully accepted.
Typical location in our previous setup:
/home/git/repositories/personal/mi-proyecto.git/hooks/post-receive
Key advantages of using this hook:
- It doesn’t block the developer: the push is already complete when the script starts running
- Decoupled logic: perfect for builds, migrations, restarts, or anything else you need
- Zero external dependencies: pure Bash (or whichever language you prefer)
Important note about the examples below
Cases A, B, and C are heavily simplified versions meant only to explain the mechanics. In production, you will almost always want to filter by branch (deploying only
main/master/ whatever you use). For that, refer to the recommended complete script at the end of this article.
Typical use cases (and their basic scripts)
Case A – Static website (HTML + CSS + JS)
Direct deployment using a forced checkout.
#!/usr/bin/env bash
unset $(git rev-parse --local-env-vars)
REPO="/home/git/repositories/personal/mi-web.git"
WEBROOT="/var/www/mi-web"
git --work-tree="$WEBROOT" --git-dir="$REPO" checkout -f main
echo "Desplegado en $WEBROOT → $(date)"
Case B – Frontend with a build step (Node, React, Vite, etc.)
Checkout -> clean install -> build -> atomic rsync.
#!/usr/bin/env bash
unset $(git rev-parse --local-env-vars)
REPO="/home/git/repositories/personal/mi-app.git"
WORKDIR="/srv/builds/mi-app"
WEBROOT="/var/www/mi-app"
mkdir -p "$WORKDIR"
git --work-tree="$WORKDIR" --git-dir="$REPO" checkout -f main
cd "$WORKDIR"
npm ci --prefer-offline --no-audit --progress=false
npm run build
rsync -a --delete "$WORKDIR/dist/" "$WEBROOT/"
echo "Build y rsync completados → $(date)"
Case C – Backend (Python, PHP, Go, etc.)
Code update + dependencies + service restart.
#!/usr/bin/env bash
unset $(git rev-parse --local-env-vars)
REPO="/home/git/repositories/personal/mi-api.git"
APPDIR="/srv/mi-api"
git --work-tree="$APPDIR" --git-dir="$REPO" checkout -f main
cd "$APPDIR"
pip install -r requirements.txt --quiet --no-cache-dir
# Requiere configuración en sudoers (ver más abajo)
sudo systemctl restart mi-api.service
echo "API actualizada y servicio reiniciado → $(date)"
Security and best practices (don’t skip this part)
1. Elevated permissions with sudoers (minimal and specific)
The git user normally cannot restart services or modify certain folders. The proper approach is to create a file inside /etc/sudoers.d/:
# /etc/sudoers.d/git-deploy (editar con visudo -f)
git ALL=(root) NOPASSWD: /bin/systemctl restart mi-api.service
git ALL=(root) NOPASSWD: /bin/systemctl restart nginx
Important: Never use NOPASSWD: ALL or git ALL=(ALL) ALL. Restrict permissions strictly to the exact command you need.
2. Secrets and environment variables
Never keep .env files inside your Git repository. Load them from a secure location instead:
# Al inicio del hook
source /etc/git-deploy/mi-proyecto.env # 600, owner git
3. Logging (essential to keep your sanity)
Redirect all output to a dedicated log file:
exec >> /var/log/git-deploy/mi-proyecto.log 2>&1
echo "=== Inicio deploy $(date '+%Y-%m-%d %H:%M:%S') ==="
# … resto del script …
echo "=== Fin deploy ==="
Create the directory and assign proper permissions:
sudo mkdir -p /var/log/git-deploy
sudo chown git:git /var/log/git-deploy
sudo chmod 750 /var/log/git-deploy
Common issues and how to fix them
“Ghost” Git environment -> strange environment variables breaking everything
Solution: always add this line at the top of your script:
unset $(git rev-parse --local-env-vars)
Command not found (npm, pip, node, etc.)
The hook does not load .bashrc or .profile. Explicitly define PATH:
export PATH="/usr/local/bin:/usr/bin:/bin:/home/git/.nvm/versions/node/v20.x.x/bin"
Permissions on the target directory
Ensure the target folder (/var/www/..., /srv/...) belongs to the git group or has the appropriate permissions configured.
The script you should actually use (multi-branch + logging + security)
#!/usr/bin/env bash
set -euo pipefail
# ────────────────────────────────────────────────
# Configuración básica
# ────────────────────────────────────────────────
export PATH="/usr/local/bin:/usr/bin:/bin"
unset $(git rev-parse --local-env-vars) 2>/dev/null || true
REPO="/home/git/repositories/personal/mi-proyecto.git"
WEBROOT="/var/www/mi-proyecto" # o /srv/mi-app, etc.
LOGFILE="/var/log/git-deploy/mi-proyecto.log"
exec >> "$LOGFILE" 2>&1
echo "=== Deploy started $(date '+%Y-%m-%d %H:%M:%S') ==="
# post-receive recibe por stdin: oldrev newrev refname
while read -r oldrev newrev refname; do
# Ignoramos deletes y tags por simplicidad
[[ "$newrev" = "0000000000000000000000000000000000000000" ]] && continue
BRANCH=$(git rev-parse --symbolic --abbrev-ref "$refname" 2>/dev/null || echo "")
if [[ "$BRANCH" == "main" ]]; then
echo "→ Push a main detectado. Desplegando..."
git --work-tree="$WEBROOT" --git-dir="$REPO" checkout -f main
# Aquí puedes poner build, migraciones, npm ci, etc.
# cd "$WEBROOT" && npm ci && npm run build
# cd "$WEBROOT" && pip install -r requirements.txt --quiet
# sudo systemctl restart mi-proyecto.service
echo "→ Despliegue finalizado OK"
else
echo "→ Rama '$BRANCH' ignorada (solo main despliega)"
fi
done
echo "=== Deploy finished $(date '+%Y-%m-%d %H:%M:%S') ==="
Wrapping up
A well-built post-receive hook is one of the most satisfying things you can set up on a personal server: lightweight, predictable, inexpensive, and 100% under your control.
Granted, once a project grows to multiple developers or requires staging, production, and review environments, you will likely migrate to GitHub Actions, Woodpecker, Drone, or similar CI/CD tools. But for the 80–90% of projects running on a budget VPS (blogs, internal tools, MVPs, side projects), pure Git paired with a smart hook remains unbeatable in simplicity and speed.
Try it on a small project first. Once you have it up and running, you’ll wonder why you didn’t do it sooner.