← Lessons

quiz vs the machine

Gold1320

Frontend

Git Hooks Pre Commit

Run checks automatically before a commit lands locally.

4 min read · core · beat Gold to climb

What a pre commit hook is

A pre commit hook is a script git runs before recording a commit. If it exits with an error, the commit is blocked. This catches problems on the developer machine before they reach the shared branch.

What to run

  • Format and lint only staged files so the hook stays fast.
  • A quick type check on changed code if it is fast enough.
  • Block accidental secrets or huge files.

Keep it fast

A slow hook tempts people to bypass it. Run checks on the staged subset rather than the whole repo, and push heavier suites to CI.

Hooks are a convenience, not a guarantee

Anyone can skip a hook with a no verify flag, so the real enforcement still belongs in CI. Treat hooks as a fast local helper that shortens the feedback loop.

A good hook fixes what it can automatically and only blocks on real problems.

Key idea

Pre commit hooks run fast checks on staged files to catch issues early, but CI remains the real gate.

Check yourself

Answer to earn rating on the learn ladder.

1. Why should a pre commit hook run only on staged files?

2. Why is CI still needed despite pre commit hooks?