Catch mistakes in a Git hook before CI ever runs

A pre-commit, commit-msg, and pre-push hook catch a lot of broken work on your laptop. CI should confirm that, not be the first place a typo shows up.

A continuous integration run is a slow way to find out you forgot a trailing comma. The cheaper place to catch that is on the machine that made the commit, in a Git hook, before the change leaves your laptop.

I keep three hooks on the repos where it matters: `pre-commit`, `commit-msg`, and `pre-push`. Husky is a convenient way to store those hooks in the repo so everyone gets them, but the useful part is the hook itself, not the tool around it.

What each hook is for

`pre-commit` runs on the files you are about to commit. This is the right place for a formatter and a fast linter. If the hook edits files, it should fail when it had to change something, so you can look at the diff and stage the result yourself. A hook that silently restages formatted files will surprise you later.

`commit-msg` reads the message you wrote. If you use Conventional Commits, this is where you reject a subject that will not parse. The hook should print the format it expected. A silent rejection feels like Git is broken.

`pre-push` is the last local gate. This is where I run the tests that are still cheap enough to finish while you wait: unit tests, a typecheck, maybe a narrow integration suite. I do not put a full browser run or a production build here unless the repo is small enough that it stays quick. A hook people start skipping is worse than a hook you never added.

How to keep the hooks honest

Git will run whatever is in `.husky/` or `.git/hooks`. A few habits keep that from turning into a second, worse build system:

  • Keep the hook scripts in the repo and short. If a hook needs a complicated pipeline, put that pipeline in a package script and have the hook call the script.
  • Fail with a clear message and a non-zero exit. Do not swallow the underlying tool's output.
  • Do not add a repo-level escape hatch that disables hooks for everyone. If someone needs to recover a broken tree, they can do that on their own machine, once, and then fix the hook.
  • Run the same checks in CI. The hook is a courtesy to the person writing the change. CI is the record that the change actually passed on a clean machine.

A small `pre-commit` sketch

This is a Husky-style `pre-commit` that only runs a project script. Adjust the script name to whatever your repo already uses:

#!/usr/bin/env sh
set -e
pnpm lint

If you want to lint only staged files, do that inside the `pnpm lint` script with the tool's own staged-file support, not by copying `git diff` output into a one-off shell loop. The hook should stay boring.

What I do not put in a hook

Secrets scanning can live here if the scanner is fast and already part of the repo. Anything that needs network access, a production token, or a long Docker build does not. Those belong in CI, where the environment is explicit and the logs are kept.

The point of the hook is to shorten the loop between making a mistake and seeing it. Once it takes longer than a sip of coffee, people look for a way around it, and then you are back to waiting on CI.

Back to Journal