Video in progress

Staff 🌿 Git

Where should policies (commit format, file size, protected paths) be enforced: in a client hook, a server-side pre-receive hook, or in CI?

A client hook is only for quick feedback and is not reliable. A pre-receive hook: the server rejects the push and it cannot be bypassed, but it slows down every push and is hard to manage. CI and branch protection are the best place for most rules. I deliberately split rules across these three layers.

📖 Answer

Each layer has a different role:

Layer Strength Weakness
Client hook fast, local --no-verify, may not be installed
Pre-receive mandatory, on every push a slow hook stops every push; often unavailable on SaaS
CI + protection full context, logs, can be rerun the bad code has already reached the branch

In pre-receive I put only things that are cheap and have "never any exceptions":

  • a file size limit (for example, no blob over 10 MB);
  • a secret scanner (push protection), because once it reaches a branch, it has spread;
  • forbidden paths or file types.

These must run in seconds. Compilation or tests in pre-receive is a bad idea: the server gets loaded, and a single hung hook stops the whole company.

Everything else, commit message format, code formatting, tests, coverage, license checks, goes in CI and blocks the merge into main through branch protection. Here explaining the error, rerunning, and granting an exception are also easy.

I present client hooks as a convenience of "you find out sooner", using the same script as CI, so that two different rules do not give two different results.