A lot of teams wire their rules into git hooks: a pre-commit hook that blocks secrets, a pre-push hook that runs the tests, a hook that enforces the commit-message format. It feels like a control, and plenty of security write-ups treat it as one. It isn't. A hook that runs on a developer's laptop is a convenience, not a boundary, and the same write-ups usually get why wrong too.
Problem
Two assumptions about client-side git hooks are common, and both are false.
"Hooks travel with the repository." They don't. Client-side hooks live in .git/hooks, which is not part of the clone. Clone a repo and your colleague's carefully written pre-commit hook is simply not there; all you get are the disabled *.sample files from your git templates. So a rule that lives only in a hook protects exactly the machines that installed it by hand, and nothing else, including CI.
"A hook is something a developer can't get around." Any hook is skippable with git commit --no-verify / git push --no-verify, or by deleting the file. A check that runs on the machine of the person you are checking is advisory by construction.
The mirror image is overstated just as often: "cloning a hostile repo runs its hooks and infects you." There is no pre-clone git hook, and because hooks are not installed by clone, a normal clone does not execute the repo author's hooks. Git briefly added a defence that blocked a repo-supplied post-checkout hook from running during clone, then reverted it, because Git LFS legitimately installs a post-checkout hook that way.
Net result: if your policy lives in client hooks, it is not enforced; and if your threat model is "cloning runs the attacker's code," that is mostly not how git works.
Solution
Put enforcement where the developer cannot bypass it: on the server. Keep client hooks for fast feedback only, and only if you distribute them on purpose.
1. Enforce the hard rules on the git server
This is Bitbucket in the examples, but the principle is identical on GitHub (branch protection / rulesets) and GitLab (protected branches / push rules): the forge is the gate.
On a self-hosted Bitbucket Data Center (plain Bitbucket Server reached end of life on 15 February 2024 — see Further reading — so if you are still on it, that is its own finding):
- Branch permissions are the real lock. Restrict who may push to and merge into
mainand your release branches. This is what stops a direct push and forces changes through a pull request. It is configured per repository or per project under Repository settings → Branch permissions. - Merge checks enforce "needs N approvals", "builds are green", or a custom check when a pull request is merged in Bitbucket. The catch worth knowing: a merge check does not stop someone merging the branch locally and pushing the already-merged result. For that you need a repository hook (the server-side
pre-receiveequivalent), which runs on every push. For full coverage use branch permissions plus a repository hook, not merge checks alone.
If you run a plain git server rather than Bitbucket, the same gate is a pre-receive hook in the bare repo. A minimal one that refuses direct pushes to protected branches:
#!/usr/bin/env bash
# Server-side: reject direct pushes to protected branches. Runs for every push,
# so it also catches a branch that was merged locally and pushed.
set -euo pipefail
protected='^refs/heads/(main|master|release/.*)$'
while read -r oldrev newrev refname; do
if [[ "$refname" =~ $protected ]]; then
# allow the branch to be created/updated only by the CI service account
if [[ "${USER:-}" != "ci" ]]; then
echo "Direct pushes to $refname are not allowed. Open a pull request." >&2
exit 1
fi
fi
done
On Bitbucket Data Center you do not drop shell scripts in like this; the equivalents are branch permissions and the repository-hook plugin module (the old pre-receive-hook module was deprecated back in Bitbucket Server 5.0). On Bitbucket Cloud the equivalents are branch restrictions and required merge checks.
2. If you want client-side hooks, distribute them deliberately
Client hooks are genuinely useful for fast feedback: format on commit, lint, scan for obvious secrets before the push even starts. Just do not confuse that with enforcement, and remember .git/hooks is not cloned, so you have to ship them.
Use the pre-commit framework: commit a config, and each developer runs pre-commit install once.
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files
- repo: https://github.com/gitleaks/gitleaks
rev: v8.21.2
hooks:
- id: gitleaks
Or keep hooks in a tracked directory and point git at it (git 2.9+), which also version-controls them:
git config core.hooksPath .githooks # commit the scripts under .githooks/
Both are opt-in per machine and both are skippable with --no-verify. That is fine, because the server is the real gate and CI runs the same checks a second time.
3. Patch git, and don't trust an untrusted clone
Cloning does not run the repo author's hooks, but neighbouring operations do run code you might not expect, and that is the part worth real caution:
.gitattributesfilters andgit-lfssmudge filters run on checkout.- Submodule URLs,
core.fsmonitorand a few config values have had remote-code-execution CVEs over the years. Keep git up to date. - Don't run
make,npm install, or your editor's "trust this workspace" on a clone you haven't read.
Test it
- Direct push is blocked. As an ordinary user,
git push origin main. Branch permissions should reject it. - The local-merge bypass is closed. Merge a pull request that fails a merge check — Bitbucket blocks it. Now merge that same branch locally and push it. A merge check alone will not stop this; confirm your repository hook / branch permissions do.
- Hooks really don't travel. Clone the repo into a fresh directory and look at
.git/hooks: only*.samplefiles.git config --get core.hooksPathtells you whether you've opted into shared hooks. - Shared hooks work for everyone.
pre-commit run --all-filesfrom a clean checkout afterpre-commit install.
What we see in projects
- "We have a pre-commit hook for that." It is installed on three of eight laptops and on no CI runner, and the one time it mattered the author used
--no-verify. Move the check to CI and to a server gate. - Merge checks without branch permissions. Pull requests require two approvals, but nobody restricted who can push, so a local merge and a plain
git pushwalks straight past the check. Add branch permissions or a repository hook. - Secret scanning only client-side. The one machine without the hook commits a key, and it is in history forever. Scan for secrets on the server (push rule) and in CI, not only in
pre-commit. - Still on Bitbucket Server. End of life since February 2024, so no security fixes. Plan the move to Data Center or Cloud.
Further reading
- Pro Git — Customizing Git: Git Hooks: client-side hooks are not copied on clone, and server-side hooks are where you enforce policy.
- githooks(5): the actual list of hooks git has — note there is no
pre-clone. - Bitbucket Data Center: Using branch permissions and the hooks and merge checks guide: why a merge check and a repository hook do different jobs.
- Bitbucket Server end-of-support announcement: the February 2024 end-of-life date.
- pre-commit: a clean way to share client-side hooks across a team.
All files from this article, ready to copy and linted on every change: GitHub ↗