Git Hooks

Learn what a Git hook is, where it runs, and two ways of deploying an application with a git push.

Presentation

You will need

  • A Unix CLI
  • Git

Recommended reading

What is a Git hook?

A Git hook is a program that Git runs when a certain action happens: before a commit, after a push has been received, and so on. A hook can be any kind of executable file: a shell script, a Python script, a compiled program, etc.

Git finds a hook by its name, in the hooks directory of a repository’s Git directory (.git/hooks in a normal repository). A file named pre-commit in that directory is run before each commit, a file named post-receive after each push the repository receives. Git only runs a hook that is executable.

Every repository created by git init or git clone starts with example hooks:

$> cd /path/to/a/git/repository

$> ls .git/hooks
applypatch-msg.sample      pre-merge-commit.sample    push-to-checkout.sample
commit-msg.sample          pre-push.sample            sendemail-validate.sample
fsmonitor-watchman.sample  pre-rebase.sample          update.sample
post-update.sample         pre-receive.sample
pre-applypatch.sample      prepare-commit-msg.sample
pre-commit.sample

$> cat .git/hooks/pre-commit.sample
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
...

These examples are executable, but Git never runs them: their .sample extension means that their names are not the names Git looks for. Removing the extension would turn one into a real hook.

Creating a hook

Create a new repository:

$> cd /path/to/projects
$> mkdir git-hook-example
$> cd git-hook-example
$> git init

Create a hook script named pre-commit, which Git will run before every commit, and make it executable:

$> echo '#!/bin/bash' > .git/hooks/pre-commit
$> echo 'echo You are about to commit...' >> .git/hooks/pre-commit
$> chmod +x .git/hooks/pre-commit

You now have a pre-commit hook:

$> cat .git/hooks/pre-commit
#!/bin/bash
echo You are about to commit...

Make a commit. Git runs the hook first, and its output appears in your terminal before the commit’s:

$> echo content > file.txt
$> git add .
$> git commit -m "Test"
You are about to commit...
[main (root-commit) 14b306d] Test
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt

What kinds of hooks are there?

There are hooks for most of what Git does. Some examples:

Hook When Git runs it Example usage
pre-commit Before a commit is made Check that the code follows the coding style.
commit-msg After the commit message is written Check that the message follows a convention.
pre-push Before a push sends commits Run the tests before sharing the code.
pre-receive When a push arrives, before it is taken Refuse commits that break the project’s rules.
post-receive After a push has been taken Deploy the new version of an application.

The git help hooks command lists them all, with when each one runs.

Where does a hook run?

A hook runs on the machine where Git performs the action, in the repository that performs it:

  • The hooks of a commit (pre-commit, commit-msg) and of a push being sent (pre-push) run on your computer, in your repository, because that is where you commit and push from. They are called client-side hooks.
  • The hooks of a push being received (pre-receive, post-receive) run on the machine that receives the push, in the repository you push to. They are called server-side hooks.

When you push to a repository on a server, Git on the server runs its hooks. What a hook prints there is sent back to your terminal, on lines that start with remote:, so that you can see what happened on the other side.

Before or after the action

A hook ends with an exit status, like any other program: 0 if it succeeded, anything else if it failed. What a failure changes depends on whether the hook runs before or after the action:

  • A hook named pre-... runs before the action. If it fails, Git does not perform the action. A failing pre-commit hook prevents the commit, and a failing pre-receive hook makes the server refuse the push.
  • A hook named post-... runs after the action. The action has already happened, so a failing post-receive hook cannot undo the push: Git can only report the failure.
📚More information

This is how a Git hosting service such as GitHub can refuse some pushes, for example a push to a protected branch: its servers check the push before taking it, as a pre-receive hook would.

Hooks are not cloned

The hooks directory is in the Git directory, not in the working directory, so a hook is not part of the repository’s history. You cannot commit it, and git status never shows it. In the example repository above, the pre-commit hook exists, but Git has nothing to commit:

$> git status
On branch main
nothing to commit, working tree clean

When you clone a repository, you therefore get its commits, but never its hooks.

This is on purpose. A hook is a program that runs on your machine whenever you use Git. If hooks came with a clone, anyone publishing a repository on the Internet could make your computer run their program as soon as you committed in it.

📚More information

A team that wants to share hooks can still commit hook scripts in an ordinary directory of the repository, and have each developer tell Git to use them, with the core.hooksPath setting. Each developer has to make that choice for their own copy.

Deploying with a push

Hooks can be used to deploy an application: every time new commits are pushed, the new version is installed and started, with no other command to type. There are two common ways of doing this, depending on where the push goes. Both are in use, and each one suits some projects better than the other.

Pushing to the server

The first way is to push directly to the machine that runs the application. The repository of the deployment is a remote of your repository, as GitHub is, reached with Git over SSH. A post-receive hook on the server installs the new version and restarts the application. This is what you will do in the automated deployment exercise.

git push on the developer’s computer connects over SSH to Git on the server, which runs the post-receive hook, which restarts the application

Some platforms work the same way. Heroku, one of the first cloud platforms, made deployment with git push heroku main popular: your repository has a remote on Heroku’s servers, and the push itself builds your application. The build happens before the push is taken, as in a pre-receive hook, so its output appears in your terminal on remote: lines, and a build that fails makes Heroku refuse the push.

Pushing to GitHub, with a webhook

The second way is to push to GitHub, and let GitHub notify the machine that deploys. The push does not go to the deployment at all.

A webhook is an HTTP request that a service sends to another one when something happens. Here, after each push, GitHub sends an HTTP request to an address chosen by the deployment, saying which repository received new commits. The deployment then fetches the new commits from GitHub, and builds and starts the new version.

git push on the developer’s computer goes to GitHub, which sends a webhook, an HTTP request, to the hosting platform, which then fetches the code from GitHub

This is how many hosting platforms deploy from a GitHub repository, as you will see later in this course. You connect the platform to your repository once, and it registers its webhook with GitHub for you.

Warning

A webhook is not a Git hook, despite the similar name. A Git hook is a program that Git runs. A webhook is an HTTP request that a web service sends. Git itself plays no part in it.

Comparing the two

The two ways do the same thing, a deployment on every push, but not in the same place:

Push to the server Push to GitHub, with a webhook
Where your push goes To the deployment, over SSH To GitHub only
What triggers the deployment A Git hook, on the server An HTTP request from GitHub
Who can deploy Whoever can connect to the server Whoever can push to the repository on GitHub
Where you see the deployment’s output In your terminal, on remote: lines In the platform’s web interface
Can a failed deployment refuse a push Yes No: GitHub has taken the push before the deployment
How the deployment gets the code It receives it from you It fetches it from GitHub

When you push to your server, GitHub and your server are two separate remotes. Each one has what you pushed to it, and nothing else. When a platform deploys from GitHub, you only push to GitHub, and the deployment follows it.

References