Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Run a check when you push with Git

After this lesson: I can make Git run a check before a push goes through.

60 min lesson13 commandsGit and Gitea lab: learn clone, commit and push on a Git server of your own

Stop a broken change before it is shared

A shop lists its prices in a file. Someone deletes the tea price by mistake and pushes. Now the whole team has a broken list.

A check that runs when you push would have said no. Real teams use checks like this all day.

By the end of this lesson

Note

I can make Git run a check before a push goes through.

Key words

WordWhat it means
Hooka script Git runs at a set moment
Checka test that passes or fails
Exit statusthe number a program ends with, 0 for success
CIa server that runs checks on every push
Runnerthe machine that carries out a CI job

The cells students fill in are left blank in this preview.

A hook is a script Git runs

Git looks in the folder .git/hooks for scripts with set names. A script called pre-push runs just before a push. If it ends with a non-zero exit status, Git stops the push.

The script must be executable. Here the check looks for the tea price in prices.txt.

Make a shared folder to push to

Command
git init --bare -b main shared.git

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Clone it as shop

Command
git clone shared.git shop

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Make a file with two prices

Command
printf 'Latte 3.20\nTea 2.10\n' > shop/prices.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Write the check

Command
printf '#!/bin/sh\ngrep -q "Tea" prices.txt || { echo "check failed: no Tea price"; exit 1; }\necho "check passed"\n' > shop/.git/hooks/pre-push

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Make the check executable

Command
chmod +x shop/.git/hooks/pre-push

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Stage the file

Command
git -C shop add prices.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Commit it

Command
git -C shop commit -m "Add prices"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Push and watch the check run

Command
git -C shop push -u origin main

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A failing check refuses the push

Now break the file on purpose. Delete the tea line and commit. The check fails, and Git refuses the push, so the broken commit stays on your computer.

You then cancel the bad commit with git revert, which you met in the lesson on undoing, and push again.

Delete the tea line

Command
sed -i '/Tea/d' shop/prices.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Commit the mistake

Command
git -C shop commit -am "Remove tea by mistake"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Try to push it

Command
git -C shop push

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Cancel the mistake

Command
git -C shop revert --no-edit HEAD

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Push again

Command
git -C shop push

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think a hook is part of the project, so the whole team gets it. It is not. A hook lives in .git, and a clone does not copy it. Anyone can skip it with --no-verify. A team needs the same check on the server.

CI runs the same check on the server

CI means continuous integration: a server that runs checks every time somebody pushes. You write the steps in a file kept in the repository, so every clone has the same rules.

In Gitea the file lives in .gitea/workflows. The server hands each job to a runner. The result shows on the pull request, so a reviewer sees a failure at once.

Worked example: a workflow file

Line of the fileWhat it says
`name: check`the name shown on the page
`on: [push]`run on every push
`runs-on: ubuntu-latest`the kind of machine to use
`- uses: actions/checkout@v4`fetch the project files first
`- run: grep -q Tea prices.txt`the check itself, the same test as the hook

The cells students fill in are left blank in this preview.

The lab's Gitea has no runner

This file does the job of the hook. Each step runs in order, and a step that exits non-zero fails the job.

The lab's Gitea accepts the file and lists a run for every push, but the run stays queued. No runner is attached, so nothing carries it out. A real team attaches one.

About this lab and its source: Git and Gitea lab: learn clone, commit and push on a Git server of your own

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.