Enforcing Commit Rules at commit-msg, Not at Push
A gate that only lived at pre-push caught violations after history was written. Moving it to commit-msg, one policy home, and fail-closed secret scanning.
TL;DR
A pre-push hook enforced a commit-message trailer rule, but rejecting pushes after history exists means fixing requires a rebase. The fix moved the same rule to a commit-msg hook, reading exemptions from shared config, so bad commits get amended instantly instead of rewritten. Pre-push remains solely as a fail-closed secret-scanning gate using gitleaks.
The push was rejected that night by a commit-discipline gate. The cause was plain: the commit being pushed touched the gate's own files without a Task: trailer. What made it painful was not the rejection but its timing. It came from the pre-push hook. Local history was already written, and the only way out was a rebase to fix an old commit message.
The assumption had been simple: one rule at one checkpoint would keep history clean. In reality, the gate worked and enforced the right thing, just wired at the most expensive point to fix. Once a commit is in history, correction stops being an ordinary operation and becomes history rewriting.
Moving the Rule to the Moment a Commit Is Created
The fix was not a new rule but a relocation of the same one to a commit-msg hook. All hooks now live in one directory, and core.hooksPath points there [2]. The Git manual describes this setting as the way to configure hooks centrally instead of per repository [2]; both absolute and relative values are accepted, and a relative path is resolved from the directory where the hook runs [2]. One detail is easy to miss when moving between machines: hooks without the executable bit are ignored [1].
Git provides two more conveniences that keep this hook thin. Before invoking a hook, Git changes its working directory to the root of the working tree [1], so the script can simply follow that location. Reading trailers needs no hand parsing either: git interpret-trailers --parse prints the valid key-value pairs, since a trailer is a key-value pair with a colon separator and its block must be preceded by a blank line [3].
git config core.hooksPath .githooks
git interpret-trailers --parse <<'MSG'
fix: adjust gate
Task: T-0169
MSG
The second command prints Task: T-0169. The check is not about the message alone. The hook reads the staged file list, matches it against the exemption globs, and only commits touching work paths must carry the trailer; commits touching only exempt files pass without extra conditions. With the same rule at commit-msg, a bad commit is now rejected before it exists. The rejection says it explicitly: "pm-gate (commit-msg): no-trailer — this commit touches work paths … without a 'Task:' trailer". The repair is trivial: amend the message of a commit just created. No history to rewrite.
One Policy, Thin Hooks
The classic hook trap is copying rules into the script. Once the exemption list changes in one place, its copies age on their own. This hook defines nothing: the exemption globs are read from the project config file, and the size caps for direct paths are imported from the same lint module used at push time. One definition, two execution points.
Portability is the second lesson. Client-side hooks are not copied when a repository is cloned [4]; the old pre-push hook even stored an absolute path owned by one machine, which broke the moment the repository moved. The new version resolves the repository root at run time with git rev-parse --show-toplevel, and every file carries a note that it is managed by the internal card manager and refreshed by its upgrade command. Two execution points for one rule usually drift into competing copies; here only the execution order is duplicated, while the policy stays in one config file and one shared module.
Two Gates, Two Moments, Fail Closed
Pre-push stays, but its role shifts to a safety net for what a commit-time check cannot see: secrets. The pre-push hook receives the list of refs about to be updated through stdin, and a non-zero exit aborts the push [4]. For every ref pair, the diff between the remote and local state is streamed into gitleaks, a detector for secrets in git repos, files, and stdin streams, with three scanning modes: git, dir, and stdin [5].
The gitleaks flag surface moves between versions; a note in the local code mentions that a recent release dropped the old stdin flag, so the invocation is tested explicitly rather than assumed. The more important decision is in the design: this gate fails closed. When the gitleaks binary is missing, the push is marked as a violation instead of passing with a warning. A missing scanner must not mean a missing check.
The final order looks like this: message policy runs at commit-msg, where history is still cheap to fix with a single amend; secret scanning runs at pre-push as the last gate; and both read their rules from one source. A rule wired at the cheapest point to correct turns a rejection from a small crisis into a one-command fix.