Sub topic 1
What Git hooks are and where they live
Prerequisites
This chapter assumes you can do the following:
- Run the basic Git workflow:
git add,git commitandgit push. - Work comfortably in a terminal and read a short shell script.
- Explain the difference between the staging area (the index) and the working tree.
You do not need to have written a Git hook before.
What a hook is
A Git hook is a script that Git runs automatically at a fixed point in its workflow. Git looks for a file with a specific name, such as pre-commit, and runs it if it exists and is executable. The script's exit status tells Git what to do next. For many hooks, a non-zero exit status stops the operation.
This lets you attach checks to actions you already perform. For example, you can reject a commit that contains conflict markers before it ever enters the history.
Hooks can be written in any language that your machine can run. Shell, Python and Node.js scripts are all common. Git only cares that the file has the right name, is executable and returns an exit status.
Client-side and server-side hooks
Hooks fall into two groups, based on where they run.
| Kind | Runs on | Examples | Typical use |
|---|---|---|---|
| Client-side | Your own machine | `pre-commit`, `commit-msg`, `pre-push` | Fast feedback before code leaves your clone |
| Server-side | The machine that receives a push | `pre-receive`, `update`, `post-receive` | Enforcing rules for everyone who pushes |
This tutorial covers client-side hooks. They help you catch mistakes early. They do not enforce anything, because each person controls their own clone and can skip or remove the hooks. A later chapter returns to this limit and explains why a CI backstop is still needed.
Where hooks live
Every repository has a hooks directory at .git/hooks. When you run git init or git clone, Git fills it with sample files that end in .sample. Git ignores those files. A hook only becomes active when you create a file with the exact hook name and no extension.
You can see the directory yourself:
ls .git/hooksThe output includes files such as pre-commit.sample and commit-msg.sample. To activate a hook, create a file named pre-commit in the same directory and make it executable.
The .git directory is not part of the commit history. It is local to your clone, so nothing in .git/hooks is versioned or copied when someone else clones the repository. A hook you write there works only on your machine. A later chapter shows how to share hooks with core.hooksPath.
The first hook
The following script is the smallest useful hook. It runs git diff --cached --check, which reports whitespace errors and leftover conflict markers in the staged changes. If the check finds problems, the script exits with status 1 and Git aborts the commit.
#!/bin/sh
# .git/hooks/pre-commit
# Reject the commit if the staged changes contain whitespace errors or conflict markers.
if ! git diff --cached --check; then
echo "pre-commit: fix the problems above or commit with --no-verify." >&2
exit 1
fi
exit 0Save it as .git/hooks/pre-commit, then make it executable:
chmod +x .git/hooks/pre-commitNow stage a file that has trailing spaces and run git commit. Git prints the offending lines from the git diff --cached --check command and the commit does not happen. Fix the lines, stage the file again and the commit succeeds.
The sequence below shows how Git and the hook interact.
Common errors
Most first-time hook problems come from a few causes:
- The file is not executable. Git skips it, usually without a warning on some versions. Run
chmod +xon the file. - The file name is wrong.
pre-commit.shandpre-commit.sampleare never run. The name must be exactlypre-commit. - The first line is missing or wrong. Without a line such as
#!/bin/sh, the operating system may not know how to run the file. - The script ends with a successful command even though a check failed. The exit status of the last command becomes the hook's status, so exit explicitly with
exit 1when a check fails.
Summary of the model
- Git runs hooks at fixed points and uses their exit status to continue or stop.
- Client-side hooks run on your machine and give early feedback.
- Hooks live in
.git/hooks, which is local and unversioned. - A hook must have the exact name, be executable and exit non-zero to block.
Exercise
You create .git/hooks/pre-commit.sh with a working check and chmod +x it, but Git never runs it. Why, and what do you change?
Show answer
Git only runs files named exactly after the hook, with no extension. Rename the file to .git/hooks/pre-commit and keep it executable. Also confirm that it starts with a shebang line such as #!/bin/sh.
Exercise
A teammate clones the repository after you add a pre-commit hook to .git/hooks. Does the hook run for them? Why or why not?
Show answer
No. The .git directory is local to each clone and is not versioned, so the hook is not copied when they clone. The hook has to be shared another way, such as a tracked directory used with core.hooksPath.