Set up an automated deployment with Git hooks

The goal of this exercise is to deploy a new version of Guess It with a single git push from your computer to your server, and a Git hook that installs and restarts the new version for you.

πŸ“œ Legend

Parts of this exercise are annotated with the following icons:

  • ❗ A task you MUST perform to complete the exercise
  • ❓ Optional step that you may perform to make sure that everything is working correctly, or to set up additional tools that are not required but can help you, or optional extra exercises to explore the topic further
  • πŸ‘Ύ Advanced tips on how to go further (or challenges!)
  • 🏁 The end of the exercise
  • πŸ›οΈ The architecture of the software you ran or deployed during this exercise
  • πŸ’₯ Troubleshooting tips: how to fix common problems you might encounter

❗ Requirements

Make sure you have completed the TLS exercise first:

  • Guess It is deployed in /srv/guessit, a clone of your fork, as in the Git clone exercise.
  • Its configuration is in the environment, not in the code, and git status in /srv/guessit shows no changes, as at the end of the configuration exercise.
  • systemd runs it as the guessit user, as in the systemd exercise.
  • nginx serves it at a name of its own over HTTPS, such as https://guessit.jde.archidepN.ch.

Visit Guess It in your browser and check that it works.

❗ Make a change to deploy

πŸ› οΈ

Do this part of the exercise on your computer, not on your server.

Open the guessit-ex directory of your fork, on your computer, in your editor. Find the navigation bar near the end of server.js, and change the name it shows, so that you can tell which version your server runs:

  <body>
    <nav class="navbar navbar-dark mb-4" style="background-color: var(--accent);">
      <div class="container">
        <a class="navbar-brand" href="/">🎯 Guess It, deployed with Git</a>
      </div>
    </nav>

Commit only server.js. Your copy may have other changes that you have not committed, such as the password you put in schema.sql during the SFTP deployment:

$> git add server.js
$> git commit -m "Change the title"

Do not push it to GitHub yet.

❗ Add your server as a remote

Still in guessit-ex on your computer, add the deployment on your server as a new remote, named archidep:

$> git remote add archidep jde@W.X.Y.Z:/srv/guessit
πŸ’ŽTip

Replace jde with your username and W.X.Y.Z with your server’s public IP address.

The URL has the shape <user>@<host>:<path>. Git connects to your server over SSH, as the ssh command does, and authenticates you with your SSH key. It then looks for a repository at the path after the colon. This one starts with /, so it is an absolute path. A path without a leading / is relative to your home directory on the server.

Your repository now has two remotes: origin, your fork on GitHub, and archidep, your server.

$> git remote -v
archidep  jde@W.X.Y.Z:/srv/guessit (fetch)
archidep  jde@W.X.Y.Z:/srv/guessit (push)
origin    git@github.com:YOUR_GITHUB_USERNAME/guessit-ex.git (fetch)
origin    git@github.com:YOUR_GITHUB_USERNAME/guessit-ex.git (push)

❗ Push to your server

Push your main branch to your server:

$> git push archidep main
...
remote: error: refusing to update checked out branch: refs/heads/main
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
...
To W.X.Y.Z:/srv/guessit
 ! [remote rejected] main -> main (branch is currently checked out)
error: failed to push some refs to 'W.X.Y.Z:/srv/guessit'

Your server refused the push. The lines starting with remote: were printed by Git on your server, and sent back to your terminal.

main is the branch checked out in /srv/guessit, the one its files come from. By default, a push only moves the branch to the commit you pushed, and leaves the files as they are. Your deployment’s files would then no longer match its branch, so Git refuses the push instead. You will tell it to update the files as well.

πŸ“š

In Git’s message, the work tree is the working directory, and the index is the staging area. After such a push, main would point to your new commit, while the working directory and the staging area would still hold the previous version. git status on your server would then show the difference as changes made by hand, undoing your commit.

A repository that only receives pushes, such as your fork on GitHub, is usually a bare repository: a Git directory with no working directory at all, created with git init --bare. Nothing is checked out in it, so a push never has files to update. Your server’s repository cannot be bare, since its working directory is your deployment.

❗ Let a push update your deployment

πŸ› οΈ

Connect to your server with SSH for this part of the exercise.

Go into your deployment, and check that it has no changes:

$> cd /srv/guessit

$> git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

Configure the repository so that Git updates the files of the working directory when a push updates the branch checked out there:

$> git config receive.denyCurrentBranch updateInstead

Check the new setting:

$> git config receive.denyCurrentBranch
updateInstead

With updateInstead, Git updates the files only if the working directory has no uncommitted changes. Otherwise it refuses the push, since updating the files would lose those changes. Your deployment has none, because its configuration is in the environment, and no longer a change to server.js.

❗ Push again

πŸ› οΈ

Go back to your terminal on your computer, not on your server, for this part.

Push again:

$> git push archidep main
...
To W.X.Y.Z:/srv/guessit
   4ea6994..2faf028  main -> main

The push went through. Visit Guess It in your browser. Do you think it shows your new title? Why?

πŸ€”Check your prediction

No: Guess It still shows the previous title, although server.js has changed on your server. Node.js read server.js when the application started. Guess It runs in production mode, without --watch, so it keeps running the code it read until it is restarted, and nothing has restarted it.

❗ Add a post-receive hook

πŸ› οΈ

Go back to your terminal that is still connected to your server over SSH for this part.

Check that the push updated your deployment:

$> cd /srv/guessit

$> git log --oneline -1
2faf028 (HEAD -> main) Change the title

$> git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

main points to your new commit, and the files match it. What is left to do after each push is what you did by hand in previous exercises: install the dependencies, and restart the service.

A Git hook can do it for you. Git runs a post-receive hook on the repository that receives a push, after the push has updated its branches. Create it in the hooks directory of your deployment’s Git directory:

$> nano /srv/guessit/.git/hooks/post-receive

Write this script in it, then exit with Ctrl-X and save when prompted:

#!/usr/bin/env bash
set -e

# Git runs this hook in the .git directory: go to the deployment first.
cd /srv/guessit

echo Installing dependencies...
npm ci

echo Restarting Guess It...
sudo systemctl restart guessit

echo Deployment successful

The script does the same as you would by hand. set -e stops it at the first command that fails, so that a version whose dependencies could not be installed is not restarted.

The hook runs as the user who pushed: you. Restarting a service is for root, so it uses sudo, which does not ask you for a password. Guess It itself still runs as the guessit user, which cannot use sudo.

Git only runs a hook that is executable. Make it executable, and check its permissions:

$> chmod +x /srv/guessit/.git/hooks/post-receive

$> ls -l /srv/guessit/.git/hooks/post-receive
-rwxr-xr-x 1 jde jde 244 Dec  3 09:41 /srv/guessit/.git/hooks/post-receive

It must have the x (execute) permission for its owner, you.

❗ Deploy with a push

πŸ› οΈ

Go back to your terminal on your computer, not on your server, for this part.

Change the title in server.js again:

  <body>
    <nav class="navbar navbar-dark mb-4" style="background-color: var(--accent);">
      <div class="container">
        <a class="navbar-brand" href="/">🎯 Guess It, deployed by a hook</a>
      </div>
    </nav>

Commit it, and push it to your server:

$> git add server.js

$> git commit -m "Change the title again"

$> git push archidep main
...
remote: Installing dependencies...
remote:
remote: added 83 packages, and audited 84 packages in 2s
remote: ...
remote: Restarting Guess It...
remote: Deployment successful
To W.X.Y.Z:/srv/guessit
   2faf028..7c3e1d4  main -> main

The lines starting with remote: are what your hook printed, on your server.

Visit Guess It in your browser. It shows your new title: the push has deployed it.

πŸ“š

This deployment is simple, and has limits:

  • The hook is in your server’s Git directory, not in the repository, so it is not under version control, and nobody gets it with a clone.
  • Every push restarts Guess It, which is down for a short time while it starts again.
  • A version that does not work stays deployed until you push one that does.

❗ Look at what the push changed

πŸ› οΈ

Go back to your terminal that is still connected to your server over SSH for this part.

Check that systemd has just restarted Guess It:

$> sudo systemctl status guessit
● guessit.service - Guess It
     Loaded: loaded (/etc/systemd/system/guessit.service; enabled; preset: enabled)
     Active: active (running) since Thu 2026-12-03 09:44:12 UTC; 1min ago
...

Look at the files of your deployment:

$> ls -l /srv/guessit
total 72
-rw-r--r--  1 jde jde  2247 Nov 12 09:31 README.md
drwxr-xr-x 83 jde jde  4096 Dec  3 09:44 node_modules
-rw-r--r--  1 jde jde 36900 Nov  5 09:15 package-lock.json
-rw-r--r--  1 jde jde   484 Nov  5 09:15 package.json
-rw-r--r--  1 jde jde  1336 Nov  5 09:15 schema.sql
-rw-r--r--  1 jde jde 14270 Dec  3 09:44 server.js

The push brought a new server.js. Can the guessit user modify it?

πŸ€”Check your answer

No. Git wrote the new server.js on your server as you, the user who pushed, so the file is yours, as it was when you updated it with git pull. The guessit user reaches it as β€œother”, which may read it (r--) but not modify it, as in the systemd exercise.

❗ Keep GitHub up to date

πŸ› οΈ

Go back to your terminal on your computer, not on your server, for this part.

Open your fork on GitHub. Does it have your new title?

πŸ€”Check your answer

No. You pushed your two commits to your server, not to GitHub. Two remotes of the same repository know nothing about each other: each has only what you pushed to it. git status shows it:

$> git status
On branch main
Your branch is ahead of 'origin/main' by 2 commits.
  (use "git push" to publish your local commits)
...

Push your commits to GitHub as well:

$> git push origin main

🏁 What have I done?

You have made git push deploy Guess It. A commit you push to your server runs there a few seconds later, with nothing typed on the server: the last manual step of the deployment is gone. You can now deploy a new version of Guess It from your machine without logging in to your server.

A remote is only a URL. Your deployment is a remote of your repository, as your fork on GitHub is, reached over SSH with the same key you use to log in. The path after the colon is on the server: absolute with a leading /, relative to your home directory without one.

A push changes files, not processes. Guess It read its code when it started, and runs in production mode, so it kept running the old version until something restarted it.

A hook is a program Git runs at a given moment. post-receive runs on the server, after a push, as the user who pushed. Your hook does what you did by hand: it installs the dependencies, then restarts the service. What it prints comes back to your terminal, as the lines starting with remote:.

The files a push brings are yours, as they were with git pull. Guess It runs as guessit, so it can still read its code but not change it. Restarting the service needs sudo, which you have and the application does not.

Your server runs only what you have committed and pushed to it. Pushing to your server does not push to GitHub: each remote is only as up to date as your last push to it.

πŸ›οΈ Architecture

This is a simplified architecture of the main running processes and communication flow at the end of this exercise. It has not changed since the TLS exercise: the processes of a push are short-lived, and the hook replaces Guess It’s process with a new one. Step through the diagram to see them.

Guess It deployed by a push to the server Your Computer Server Internet systemd Init process, PID 1 /run/systemd/private Browser https://guessit.jde.archidep2.chβ–ˆ nginx Reverse proxy 443 Guess It node server.js 3000 Guess It node server.js 3000 PostgreSQL 5432 Terminal & Shell $>git push archidep mainβ–ˆ Git git push archidep main SSH Client SSH Server 22 SSH Session Git git-receive-pack Git Hook post-receive npm npm ci systemctl sudo systemctl restart guessit https://guessit.jde.archidep2.ch http://127.0.0.1:3000 postgresql://guessit:pw@localhost:5432/guessit jde@W.X.Y.Z Legend Process Communication Control Network Boundary Listening Port commitrestart guessitSIGTERMdoneDeployment successfulGET /GET /SELECT …rowsHTMLHTML

πŸ“ Where is everything?

These are the files and directories of your server that matter for this deployment.

Path What it is What you did Who uses it
πŸ“ /srv/guessit changed Your deployment: a clone of your repository, which now also receives your pushes Pushed to it from your computer, with git push archidep main You, to deploy. The guessit user reads it
πŸ“„ /srv/guessit/.git/config changed The configuration of your deployment’s repository, which now lets a push update it Set receive.denyCurrentBranch to updateInstead with git config Git, when it receives a push
πŸ“„ /srv/guessit/.git/hooks/post-receive new The hook: what to do after each push Wrote it with nano, and made it executable with chmod +x Git, which runs it as you after each push
πŸ“ /srv/guessit/node_modules changed The dependencies of the application Nothing directly: the hook installs them again with npm ci on each push node, when the code loads a dependency

You can explore all of this yourself: ls -l shows what a directory contains, with its permissions, and cat, head and tail what a file contains, as in the command line cheatsheet. For example:

# The configuration of your deployment's repository,
# with the setting you added.
$> cat /srv/guessit/.git/config

# The hooks of the repository: Git only runs the
# executable ones without the .sample extension.
$> ls -l /srv/guessit/.git/hooks

πŸ’₯ Troubleshooting

Here’s a few tips about some problems you may encounter during this exercise.

πŸ’₯ ! [remote rejected] main -> main (branch is currently checked out)

Git on your server still refuses to update the branch checked out in /srv/guessit. Check the setting on your server:

$> cd /srv/guessit
$> git config receive.denyCurrentBranch
updateInstead

If it prints nothing, or something else, set it again as in Let a push update your deployment, from inside /srv/guessit. Run elsewhere, git config changes another repository, or fails with fatal: not in a git directory.

πŸ’₯ ! [remote rejected] main -> main (Working directory has unstaged changes)

A file of /srv/guessit has a change that is not committed, which the push would have lost. On your server, see which one:

$> cd /srv/guessit
$> git status

If it is a change you do not need, such as the database password you once put in server.js, discard it with git restore, as in the configuration exercise, then push again. Do not commit on your server: your code changes are made on your computer.

πŸ’₯ does not appear to be a git repository

$> git push archidep main
fatal: 'srv/guessit' does not appear to be a git repository
fatal: Could not read from remote repository.

There is no repository at the path of your remote’s URL, on your server. Check the URL with git remote -v. The path must be /srv/guessit, with the leading /: without it, Git looks for srv/guessit in your home directory. Fix the URL, replacing jde and W.X.Y.Z with your username and your server’s IP address:

$> git remote set-url archidep jde@W.X.Y.Z:/srv/guessit

πŸ’₯ Everything up-to-date

$> git push archidep main
Everything up-to-date

Your server already has every commit of your main branch, so there is nothing to push, and the hook does not run. Commit your change, or make a new change and commit it, before pushing. git status shows whether you have changes that are not committed yet.

πŸ’₯ hint: The 'hooks/post-receive' hook was ignored because it's not set as executable.

Git found your hook but did not run it. Make it executable on your server, as in Add a post-receive hook:

$> chmod +x /srv/guessit/.git/hooks/post-receive

The push itself went through, so pushing again prints Everything up-to-date and still does not run the hook. Commit another change to deploy it.

πŸ’₯ My push works, but no line starts with remote:

Git did not find your hook. It must be named exactly post-receive, with no extension, in /srv/guessit/.git/hooks. Check with ls -l /srv/guessit/.git/hooks.

πŸ’₯ The hook stops before Deployment successful

A command of the hook failed, and set -e stopped the hook there, without restarting Guess It. The remote: lines just above the end show which command failed, and why: a misspelled path after cd, for example. Fix the hook on your server, then commit another change and push it to run the hook again.

πŸ’₯ The site still shows the old version

Check that the push printed remote: Deployment successful. If it did, check that systemd restarted Guess It, with sudo systemctl status guessit: the time after since must be the time of your push. Then reload the page in your browser.

πŸ’₯ 502 Bad Gateway after a push

nginx could not reach Guess It: the version you pushed does not start. See why in its logs:

$> sudo journalctl -u guessit

Fix the problem on your computer, then commit and push again.