Log inSign up

Push-to-deploy with a bare repository and Git hooks

Create a bare repository on the server with git init --bare /srv/git/app.git, add an executable hooks/post-receive script that checks out the pushed main branch into your web folder, and restart the app from that script. On your own machine add the server as a remote and deploy with git push production main; the hook output appears right in your terminal.

Topic: Git on a server · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

How does push-to-deploy work?

A bare repository is a Git repository without a checked-out working tree: it only holds the history, so it can safely receive pushes. After every push, Git runs the hook scripts in its hooks folder. A post-receive hook reads which branches changed and, for the deploy branch, writes the new files into the folder your app runs from.

The result is a deploy that needs nothing but SSH and Git on the server: no build service, no extra daemon and no open port beyond 22. It suits a single VPS running a website, an API or a bot. You need Git installed and configured on both ends, as in install and configure Git, a sudo user such as deploy, and SSH key login, covered in the SSH keys tutorial.

How do you prepare the server user and folders?

Install Git, create one folder for the bare repository and one for the deployed code, and give both to the deploy user.

# Ubuntu and Debian
sudo apt update && sudo apt install -y git

# Rocky Linux and AlmaLinux
sudo dnf install -y git

sudo mkdir -p /srv/git /srv/app/releases
sudo chown -R deploy:deploy /srv/git /srv/app

Keep the bare repository outside any folder a web server serves, so the history and hooks are never downloadable. The web server or the app process then reads from /srv/app/current, which the hook will point at the newest release. Only SSH needs to be reachable: sudo ufw allow OpenSSH on Ubuntu and Debian, or sudo firewall-cmd --permanent --add-service=ssh followed by sudo firewall-cmd --reload on Rocky and Alma. The firewall tutorial covers the full rule set.

How do you create the bare repository?

Initialise it as the deploy user with --bare and a fixed initial branch, then tighten what pushes it accepts.

git init --bare --initial-branch=main /srv/git/app.git
cd /srv/git/app.git
git config receive.denyNonFastForwards true
git config receive.denyDeletes true
ls hooks/

The two receive settings refuse forced pushes that would rewrite history and refuse deleting branches, which protects production from an accidental --force. The hooks folder contains .sample files for reference; Git only runs a hook whose name has no suffix and which is executable.

How do you write the post-receive hook?

Create /srv/git/app.git/hooks/post-receive; Git feeds it one line per updated ref with the old commit, the new commit and the ref name.

#!/usr/bin/env bash
set -euo pipefail

GIT_DIR=/srv/git/app.git
BASE=/srv/app
BRANCH=main
KEEP=5

while read -r oldrev newrev ref; do
  if [ "$ref" != "refs/heads/$BRANCH" ]; then
    echo "Received $ref, only $BRANCH is deployed."
    continue
  fi
  RELEASE="$BASE/releases/$(date +%Y%m%d-%H%M%S)-${newrev:0:7}"
  echo "Deploying $BRANCH at ${newrev:0:7} to $RELEASE"
  mkdir -p "$RELEASE"
  git --git-dir="$GIT_DIR" archive "$newrev" | tar -x -C "$RELEASE"

  # build step, adjust for your stack
  if [ -f "$RELEASE/package.json" ]; then (cd "$RELEASE" && npm ci --omit=dev); fi
  ln -sfn "$BASE/shared/.env" "$RELEASE/.env"

  ln -sfn "$RELEASE" "$BASE/current.new"
  mv -T "$BASE/current.new" "$BASE/current"
  sudo /usr/bin/systemctl restart app.service

  ls -1dt "$BASE"/releases/* | tail -n +$((KEEP + 1)) | xargs -r rm -rf
  echo "Done: $(readlink "$BASE/current")"
done

Make it executable with chmod +x /srv/git/app.git/hooks/post-receive and put the shared secrets file at /srv/app/shared/.env. Each push builds a fresh release folder from git archive, so leftovers from older versions never mix in. Swapping the current symlink with mv -T is a single rename, so requests see either the old or the new release, never half of each. The last line keeps five releases for quick rollbacks.

Is there a simpler hook for static sites?

Yes. If the content needs no build and no restart, check the branch out straight into the served folder:

#!/usr/bin/env bash
while read -r oldrev newrev ref; do
  [ "$ref" = "refs/heads/main" ] || continue
  git --work-tree=/var/www/site --git-dir=/srv/git/site.git checkout -f main
  echo "Site updated to ${newrev:0:7}"
done

Create /var/www/site first and give it to the deploy user. This form overwrites tracked files in place and does not remove files you deleted from the repository; add git --work-tree=/var/www/site --git-dir=/srv/git/site.git clean -fd after the checkout if that matters. Point Nginx or Caddy with HTTPS at the folder.

How can the hook restart a service without full sudo?

Allow the deploy user to run exactly one command as root through a sudoers drop-in, and nothing else.

sudo visudo -f /etc/sudoers.d/deploy-app
# add this single line:
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart app.service

sudo -u deploy sudo -n /usr/bin/systemctl restart app.service

The last line tests the rule without a password prompt; it must succeed or the hook will stop at the restart. The unit itself is an ordinary systemd service whose WorkingDirectory is /srv/app/current, as described in running an app as a systemd service. For PHP sites served by PHP-FPM, reload the pool instead of restarting, and clear the opcode cache if your setup needs it.

How do you push your first deploy?

On your workstation, add the server as a second remote and push the deploy branch to it.

git remote add production ssh://deploy@198.51.100.24/srv/git/app.git
git push production main

The lines starting with remote: in the push output are the echo statements from the hook, so you can watch the build and restart live. If a hook step fails, the push itself is already stored but the hook prints the error; fix it and push again, or rerun by hand: echo "0 $(git --git-dir=/srv/git/app.git rev-parse main) refs/heads/main" | /srv/git/app.git/hooks/post-receive. With a DNS name in place you can write deploy@app.example.net instead of the IP; see DNS records.

How do you guard the branch and roll back?

A pre-receive hook runs before Git accepts anything, so it can reject pushes that should never reach production.

#!/usr/bin/env bash
while read -r oldrev newrev ref; do
  case "$ref" in
    refs/heads/main|refs/heads/staging|refs/tags/*) ;;
    *) echo "Push refused: only main, staging and tags are accepted."; exit 1 ;;
  esac
done

A non-zero exit rejects the whole push. To roll back, point the symlink at the previous release and restart; no Git command is needed:

ls -1dt /srv/app/releases/*
ln -sfn /srv/app/releases/20261203-101500-3e1a9c2 /srv/app/current.new
mv -T /srv/app/current.new /srv/app/current
sudo /usr/bin/systemctl restart app.service

Then fix the problem properly with git revert and a new push, as shown in the undo guide, so the repository and the server agree again.

What goes wrong most often with deploy hooks?

  • Hook never runs: the file is not executable, has a .sample suffix, or has Windows line endings. Check with ls -l hooks/ and file hooks/post-receive.
  • "command not found" for npm, composer or similar: hooks run with a minimal environment. Use full paths or set PATH at the top of the script.
  • Permission denied writing the release: /srv/app is not owned by the user that pushes. Fix ownership with chown.
  • Submodule folders empty: git archive skips submodules. Clone with --recurse-submodules into the release instead, see Git submodules.
  • Disk filling up: lower KEEP and check space with the disk usage guide.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-2a small app plus a few kept releases1 vCPU, 2 GB RAM, 50 GB NVMe$7.20/mo$43.20 for 6 months
A2-4headroom for builds during each deploy2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months

Questions about Deploy with a bare repository and hooks

Why use a bare repository instead of pulling in the web folder?

A bare repository can accept pushes without conflicting with checked-out files, and it keeps the .git data out of the served folder. Deploys become a push from your side instead of a login and pull on the server.

Can several people deploy to the same server?

Yes. Add each person's public key to ~/.ssh/authorized_keys of the deploy user. For finer control, restrict that account with git-shell as its login shell so it can only run Git commands.

Should I run tests in the hook?

Light checks are fine in pre-receive, where a failure rejects the push. Long test suites are better run in CI before the push; the CI runners page shows how a VPS can host those jobs.

Does this work with Docker?

Yes. Replace the build and restart lines with docker compose build and docker compose up -d run in the release folder, after installing Docker Engine.

How do I deploy a staging copy from the same repository?

Add a second branch check in the hook that deploys staging into another base folder and restarts a second service, so both versions run side by side on one VPS.