Hooks and checks
Last updated
Lesson 3.4 warned about committing a file with conflict markers still in it. A hook can catch that for you: a small script Git runs at certain moments, which can stop a commit. In this lesson you'll watch a hook stop a bad commit, see how teams share hooks, and why the real guarantee is a check on the host, not on your computer.
By the end of this lesson you can
- Explain what a Git hook is and when pre-commit and commit-msg run
- Use the pre-commit framework to share hooks with a team
- Explain why hooks are a convenience and CI is the guarantee
What a hook is
A hook is a script in your repository's .git/hooks folder, named after a moment. Git runs it at that moment; if it fails, Git stops what it was doing. The two you'll meet most:
- pre-commit runs before a commit is made. It can look at what's staged and refuse: conflict markers, a file that's too large, code that fails a quick check.
- commit-msg runs after you write the message. It can refuse a message that doesn't follow the team's rules (lesson 7.2).
Others run before a push (pre-push), after a checkout, and so on. On the host, server-side hooks and checks can refuse a push itself.
A hook stops a bad commit
Kusina now has a pre-commit hook. cat .git/hooks/pre-commit shows it: it looks through the staged changes for conflict markers, and fails if it finds any.
$ git merge more-vinegar Auto-merging adobo.md CONFLICT (content): Merge conflict in adobo.md Automatic merge failed; fix conflicts and then commit the result.
4d924ecHEAD →main Use coconut vinegar in the adobo588df20more-vinegar Use more vinegar in the adobo67e435dCover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe
and 2 older commits
Now try it, and the escape hatch: git commit --no-verify (or -n) skips pre-commit and commit-msg hooks. git commit --no-verifyCareful: changes files or history, but Git can usually get it back It exists for emergencies, and it's why hooks can't be the last line of defence.
- Read the hook with
cat .git/hooks/pre-commit - Merge more-vinegar, stage the file without settling it, and try to commit
- Settle the conflict properly and commit
Working folder
no changes
Staging area
nothing staged
Repository
11 commits on main
You are in ~/kusina on main, as in lesson 3.4, but with a pre-commit hook in .git/hooks/pre-commit. more-vinegar still conflicts with main.
Commit graph
4d924ecHEAD →main Use coconut vinegar in the adobo588df20more-vinegar Use more vinegar in the adobo67e435dCover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
Sharing hooks with a team
.git/hooks is inside .git, so it isn't committed, and git clone doesn't copy it. That's deliberate: a repository you clone can't make your computer run its scripts without you agreeing. So how does a team share hooks?
The usual answer is a framework. With pre-commit (pre-commit.com), the repository has a committed file listing the checks:
repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v5.0.0 hooks: - id: check-merge-conflict - id: check-added-large-files - id: end-of-file-fixer
Each person runs pre-commit install once, and from then on every commit runs those checks. (rev pins a version of the checks; pre-commit autoupdate moves it to the newest.) Updating the file updates everyone's hooks. Other tools (Husky in JavaScript projects, Lefthook) do the same job.
Hooks are a convenience; CI is the guarantee
Hooks run on each person's computer. Someone may not have installed them, may use --no-verify, or may commit from a tool that skips them. So hooks are a convenience: they catch mistakes early, before you've pushed and waited.
The guarantee is continuous integration (CI): the host runs the same checks on every push and pull request, on its own computers, and a branch rule (lesson 7.4) refuses to merge until they pass. Nobody can skip those. Simple Software Development 7.2 covers CI.
The best setups run the same checks in both places: quickly before each commit, and for certain on the host.
Spot the trap: relying on hooks alone
Ana sets up the conflict-marker hook and tells the family: “Markers can’t get into main any more.”
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Git documentation: githooks: every hook, when it runs, and --no-verify
- Pro Git: Git Hooks: client-side hooks, and that clone doesn't copy them
- pre-commit: a framework for sharing hooks through a config file
- GitHub Docs: About status checks: checks that run on the host for every pull request