Sub topic 2
Client-side hooks and when Git runs them
This chapter assumes you know the basic Git workflow (add, commit, push) and the difference between the staging area and the working tree. You should also be comfortable writing short shell scripts.
Client-side and server-side hooks
A Git hook is a script that Git runs at a fixed point in a workflow. Hooks fall into two groups. Client-side hooks run on your own machine, during actions such as committing and pushing. Server-side hooks run on the machine that receives a push, such as a Git host.
This tutorial covers only client-side hooks. They give you fast feedback before a bad commit exists or before bad code leaves your machine.
Where hooks live
Every repository has a .git/hooks directory. Git creates it with sample files that end in .sample. Git ignores those samples. To activate a hook, you create an executable file with the exact hook name and no extension, for example .git/hooks/pre-commit.
You can list what is there:
ls .git/hooksThe commit-time hooks
Git runs several hooks in a fixed order during git commit. The table shows the ones you will use most.
| Hook | When it runs | Arguments | Can it stop the action? |
|---|---|---|---|
| `pre-commit` | Before Git asks for a message | None | Yes |
| `prepare-commit-msg` | After Git builds the default message, before the editor opens | Message file, source, sometimes a commit ID | Yes |
| `commit-msg` | After you write the message | Path to the message file | Yes |
| `post-commit` | After the commit is created | None | No |
A non-zero exit status from pre-commit, prepare-commit-msg or commit-msg aborts the commit. post-commit runs after the commit already exists, so it can only react to it, for example to send a notification.
Hooks around push and other actions
Other client-side hooks cover different points in the workflow:
pre-pushruns after Git contacts the remote and before it sends any objects. A non-zero exit cancels the push. It suits slower checks such as a test suite.pre-rebaseruns before a rebase starts and can refuse it.post-checkoutruns aftergit checkoutorgit switchupdates the working tree.post-mergeruns after a successful merge.applypatch-msg,pre-applypatchandpost-applypatchrun duringgit am.
The "post" hooks are for notification and housekeeping, such as reinstalling dependencies after a branch switch. They cannot block anything.
The flow of a commit
The diagram shows the order of hooks during a single git commit, and where each can abort.
Seeing a hook run
You can confirm that Git calls a hook with a tiny example. Create .git/hooks/pre-commit with this content:
#!/bin/sh
echo "pre-commit hook is running"
exit 0Then make it executable and commit:
chmod +x .git/hooks/pre-commit
git commit --allow-empty -m "Test the hook"You should see the message printed before the commit finishes. Change exit 0 to exit 1 and the commit is rejected.
Common mistakes
- The file has the wrong name, such as
pre-commit.sh. Git looks only for the exact hook names. - The file is not executable. Git skips it, and in some setups prints a hint.
- The hook runs in a different working directory than you expect. Git usually runs commit hooks from the top of the working tree, so use paths relative to that.
- Output goes to the wrong place. Print errors to standard error so they are visible when the hook fails.
Exercise
A teammate wants to reject commits whose message does not follow a naming format. Which hook should they write, and why can't pre-commit do this job?
Show answer
They should write commit-msg. pre-commit runs before the message is written, so the message does not exist yet. commit-msg runs after the message is entered and receives the path to the message file as its first argument.
Exercise
You add post-commit with exit 1 to a repository. Does the commit succeed?
Show answer
Yes. post-commit runs after the commit has been created, and its exit status cannot undo or block it.