Do this part of the exercise on your computer, not on your server.
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 statusin/srv/guessitshows no changes, as at the end of the configuration exercise. - systemd runs it as the
guessituser, 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
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
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?
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?
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?
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.
In this diagram, a browser reaches Guess It over HTTPS, at https://guessit.jde.archidep2.ch, through nginx, which forwards the request to Guess It on port 3000. systemd, the init process, started all three, and restarts Guess It when a push deploys a new version: step through the diagram to see the push.
You have committed a new title on your computer. On your server, systemd, the init process, started the SSH server, PostgreSQL, Guess It and nginx when the server booted. Guess It runs the previous version of its code.
You push your commit to your server with git push archidep main, in the terminal on your computer.
Git does not connect to your server itself: it starts ssh, the SSH client, with the username and IP address of the archidep remote.
The SSH client connects to port 22, where the SSH server is listening, and authenticates you with your SSH key, as when you log in.
The SSH server accepts the connection, and starts a new SSH session for it.
The SSH server hands the connection over to the new session, and goes back to waiting on port 22.
The SSH client asked to run git-receive-pack /srv/guessit rather than a shell, so the session starts it: the part of Git that receives a push into the repository at that path.
Your new commit travels over the SSH connection, from Git on your computer to Git on your server.
git-receive-pack works: it stores the commit, moves main to it, and updates the files of /srv/guessit, as updateInstead tells it to.
Once main has moved, git-receive-pack runs the post-receive hook, as you, the user who pushed.
The hook runs npm ci, which installs Guess Itβs dependencies in /srv/guessit.
npm ci exits, and the hook runs sudo systemctl restart guessit.
systemctl does not restart Guess It itself: it connects to systemd through a local socket, /run/systemd/private.
systemctl asks systemd to restart the Guess It service, and waits for the answer.
systemd sends Guess It the SIGTERM signal, which asks it to stop.
Guess It exits. It was still running the previous version of server.js, which Node.js read when Guess It started.
systemd starts Guess It again, as the guessit user. Node.js reads the new server.js, and Guess It listens on port 3000.
systemd answers systemctl that Guess It has restarted, and systemctl exits.
The hook prints Deployment successful, which travels back over the SSH connection. Git shows it in your terminal, on a line starting with remote:.
The hook exits, then git-receive-pack. The SSH connection closes, the session and the SSH client exit, and so does git push: the push is done.
Someone opens a browser on their computer.
They type Guess Itβs address into the browser, starting with https://. The browser looks up guessit.jde.archidep2.ch in the DNS, which answers with your serverβs public IP address.
The browser connects to port 443, the default port of HTTPS, where nginx listens.
The browser sends an HTTP request for the home page over the connection, encrypted, and waits for the answer.
nginx works: it decrypts the request, and picks the configuration that proxies it to http://127.0.0.1:3000.
nginx connects to Guess It on port 3000, at 127.0.0.1, the serverβs own loopback address.
nginx sends the request on to Guess It, in plain HTTP, and waits for the answer.
Guess It works: Node.js runs the new version of its JavaScript code, which needs the leaderboard from the database.
Guess It connects to PostgreSQL on port 5432, with the database URL systemd gave it from its configuration file.
Guess It sends its query over the connection.
PostgreSQL works, running the query, while Guess It waits for the answer.
The rows of the leaderboard travel back to Guess It.
Guess It works again, building the HTML page from the rows, with your new title.
The page travels back to nginx, in plain HTTP, over the connection nginx opened.
nginx encrypts the page and sends it on to the browser, over the browserβs connection.
The browser works, decrypting and displaying the page with your new title.
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.