Sub topic 7
Bypassing hooks and why CI is still required
This chapter assumes you can write a pre-commit hook, know that a non-zero exit status aborts the commit, and know how core.hooksPath shares hooks. Here you will see how hooks get skipped, on purpose or by accident, and why a server-side check has to back them up.
Hooks are a convenience, not a guarantee
Client-side hooks run on the developer's machine, under the developer's control. Git runs them only if they exist, are executable, and are found at the configured path. Anyone can skip them, so a hook gives fast feedback but cannot enforce a rule.
Ways a hook gets skipped
The deliberate way is --no-verify, or -n for short:
git commit --no-verify -m "WIP: save progress"
git push --no-verifyOn git commit, this skips pre-commit and commit-msg. On git push, it skips pre-push. It does not skip hooks that run after the fact, such as post-commit.
Hooks also go missing without anyone choosing to skip them:
- A fresh clone has no
core.hooksPathsetting, because Git config is not versioned. The tracked.githooksdirectory exists, but Git does not look in it. - The file lacks the executable bit. Git ignores it and may print a hint.
- Someone runs
git config core.hooksPath /dev/null, or points it at another directory. - A tool that commits for you, such as a bot, a web editor or a script, does not run your local hooks.
- A hook manager has its own switch. For example, the pre-commit framework honors
SKIP=trailing-whitespace git commit, and Husky honorsHUSKY=0.
# Check whether hooks are wired up in this clone
git config --get core.hooksPath || echo "core.hooksPath is not set"
ls -l .githooksLegitimate reasons to bypass
Bypassing is sometimes right. You may need to commit a work-in-progress snapshot to a private branch. A hook may fail because of a broken tool install, not because of your code. You may be committing an emergency fix while a slow check is failing for unrelated reasons.
The rule is that bypassing only skips the local feedback. The shared branch must still reject bad code, and that is CI's job.
CI is the enforcement point
A CI job runs on a machine the contributor does not control, against the commits that were actually pushed. Combined with branch protection that requires the job to pass before merging, it cannot be skipped with a flag.
Server-side Git hooks such as pre-receive can also reject pushes. Hosted services often do not let you install them, so most teams rely on CI status checks.
Run the same checks in both places
If the hook and CI use different commands, they drift apart. A change passes locally and fails in CI, or the reverse. Put the check in one script and call it from both.
#!/bin/sh
# scripts/check.sh
# Shared check: used by the pre-commit hook and by CI.
set -e
git ls-files -z -- '*.sh' | xargs -0 -r shellcheckThe hook then calls the script:
#!/bin/sh
# .githooks/pre-commit
exec scripts/check.shThe CI job calls the same script. This GitHub Actions example runs on every pull request:
# .github/workflows/check.yml
name: check
on: [pull_request]
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: sudo apt-get install -y shellcheck
- run: scripts/check.shNote that this script checks the working tree, which is fine in CI because the checkout is clean. In the local hook you would normally check the staged content, as covered in the earlier chapter on staged files. CI should check the full tree even when the hook checks only staged files.
Common errors
- Treating the hook as the only gate. Without a required CI status, the first
--no-verifyor fresh clone lets bad code in. - Forgetting the setup step. Document
git config core.hooksPath .githooksin the README, or add amake setuptarget. Otherwise new clones silently run no hooks. - Making CI weaker than the hook. CI should be a superset of the local checks.
- Making bypass the habit. If people use
--no-verifyevery day, the hook is too slow or too noisy. Fix the hook before blaming the people.
Exercise
A teammate clones the repository, commits code with trailing whitespace, and the pre-commit hook never runs. Name two likely causes and the CI-side fix that catches the problem anyway.
Show answer
Likely causes: core.hooksPath was never set in the new clone (Git config is not versioned), or the hook file is missing its executable bit. The teammate could also have used --no-verify. The fix is a CI job that runs the same shared check script on every pull request, with branch protection requiring that job to pass before merging.