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 understand the difference between the staging area and the working tree. It covers the ways a client-side hook gets skipped, why that cannot be fixed on the client, and how to pair hooks with a server-side check.
Hooks are a convenience, not a gate
A client-side hook runs on the machine of the person making the commit. That person controls the machine, the repository configuration, and the files in .git/hooks. Git does not transfer hooks when you clone or push, and it does not ask the server whether a hook ran.
So a hook can only give fast feedback. It cannot guarantee that bad code stays out of the shared repository. Any rule that must always hold needs a second check on a machine you control, such as a CI server or the Git host.
Ways a hook gets skipped
Some bypasses are deliberate and some are accidents. Both lead to the same result: a commit reaches the remote without the check.
The --no-verify flag
git commit --no-verify (short form -n) skips the pre-commit and commit-msg hooks. git push --no-verify skips pre-push.
git commit -n -m "WIP: save my work before lunch"
git push --no-verifyThis is a supported feature. The message in the first hook example, "commit with --no-verify", points to it on purpose. Sometimes you need to save broken work on a private branch, or the hook itself is faulty and blocks a fix for the hook.
Some hooks are not skipped by --no-verify. post-commit, post-checkout and post-merge still run, and their exit status cannot stop the operation. Also, prepare-commit-msg still runs on commit. Do not rely on --no-verify to skip everything.
Hooks that were never installed
A fresh clone has no custom hooks in .git/hooks, only inactive .sample files. If your team uses core.hooksPath, each person must run the git config core.hooksPath .githooks command once per clone. People forget. New contributors often do not know the step exists. A hook manager has the same gap: pre-commit install must be run in each clone.
Hooks that are not executable
Git ignores a hook file without the executable bit. On Unix-like systems it prints a hint such as The '.git/hooks/pre-commit' hook was ignored because it's not set as executable, and the commit proceeds. This often happens when a file is created on Windows or copied through a tool that drops permissions. Fix it with chmod +x and, for tracked files, record the mode:
chmod +x .githooks/pre-commit
git update-index --chmod=+x .githooks/pre-commit
git ls-files --stage .githooks/pre-commitThe last command should show mode 100755 at the start of its output.
Commits that never trigger the hook
Some operations create commits without running pre-commit:
git commit --amendrunspre-commit, butgit rebasereplays commits without running it for each one.git cherry-pick,git mergethat is fast-forwarded, andgit revertmay skip or reduce hook coverage depending on options.- Commits made by an IDE, a GUI, or a library such as libgit2 may use their own hook settings, or skip hooks entirely.
- Edits made through a web interface of the Git host never touch your local hooks.
Different configuration on each machine
core.hooksPath can be set globally, per repository, or not at all. A global value in ~/.gitconfig can silently replace the hooks directory of every repository. Check what is in effect:
git config --show-origin --get core.hooksPathIf this prints nothing, Git uses .git/hooks.
Hooks that fail open
A hook can run and still let bad code through. Typical causes:
- The script ends with
exit 0regardless of the earlier results. - A required tool is missing, and the hook treats "command not found" as success.
- The check inspects the working tree, not the staged content, so a partial
git addhides the problem. - A pipeline like
git show ":$f" | shellcheck -reports only the last command's status unless you handle it explicitly.
A common design decision is whether a missing tool should block the commit. Failing closed is safer for policy. Failing open is friendlier for contributors who have not set up their tools yet. Pick one deliberately and say so in the message the hook prints.
#!/bin/sh
# .githooks/pre-commit
# Fail closed: a missing tool blocks the commit with a clear message.
if ! command -v shellcheck >/dev/null 2>&1; then
echo "pre-commit: shellcheck is not installed. Install it or commit with --no-verify." >&2
exit 1
fi
status=0
for f in $(git diff --cached --name-only --diff-filter=ACM -- '*.sh'); do
git show ":$f" | shellcheck - || status=1
done
exit $statusSummary of bypass paths
| Path | Deliberate? | Skips which hooks | Typical fix |
|---|---|---|---|
| `git commit --no-verify` | Yes | `pre-commit`, `commit-msg` | CI check |
| `git push --no-verify` | Yes | `pre-push` | CI check |
| Hook not installed in clone | No | All custom hooks | Documented setup, setup script, CI check |
| Hook file not executable | No | That hook | `chmod +x`, record mode in Git |
| Rebase, IDE, web edit | Sometimes | `pre-commit` for replayed commits | CI check |
| Wrong `core.hooksPath` | No | All hooks in the intended directory | Inspect with `--show-origin` |
Why CI is still required
CI runs on a server that the team controls. Contributors cannot turn it off with a flag, and it starts from a clean checkout, so it does not depend on local tools, local configuration, or whether a hook was installed.
The two layers have different jobs:
- Hooks give feedback within seconds, before a commit exists. They save a round trip.
- CI gives a verdict that cannot be skipped from a contributor's machine. It is the place to run the full test suite and the same lint checks as the hooks.
Run the same checks in both places
If the hook and CI use different commands or different versions, they disagree, and people stop trusting the hook. Put the real check in a script in the repository and call it from both.
#!/bin/sh
# scripts/check.sh
# One entry point for lint checks, used by hooks and by CI.
set -eu
git ls-files -z -- '*.sh' | xargs -0 -r shellcheckThe hook runs this script, and the CI job runs the same file:
#!/bin/sh
# .githooks/pre-commit
exec scripts/check.shThis version checks the working tree, which is fine for CI because the checkout is clean. In the hook it can differ from the staged content, so for a stricter local check combine it with the staged-file approach from the earlier chapter. The CI result is the one that counts.
A CI job for the same script looks like this on GitHub Actions:
# .github/workflows/lint.yml
name: lint
on:
pull_request:
push:
branches: [main]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: sudo apt-get update && sudo apt-get install -y shellcheck
- run: scripts/check.shMake CI binding with branch protection
A CI job that nobody has to wait for is only advice. Configure the Git host so that:
- The default branch rejects direct pushes.
- Changes arrive through pull requests (or merge requests).
- The lint job is a required status check.
- Administrators are subject to the same rules, where the host allows it.
Without these settings, a contributor with write access can push straight to the main branch and skip both the hook and the job. The exact settings are named differently on each host, such as branch protection rules or protected branches.
Server-side hooks
If you host the repository yourself, you can also run a pre-receive or update hook on the server. It runs on the remote when a push arrives and can reject it. Hosted services usually do not let you install custom server-side hooks, which is why required status checks are the common option there. Server-side hooks are outside the scope of this chapter.
Handling bypasses in practice
Decide what your team's policy is and write it down.
- Allow
--no-verifyfor personal branches and work in progress, and rely on CI to block the merge. - Keep hooks fast. Slow hooks are the main reason people bypass them. The later chapter on speed and scope covers this.
- Print an actionable message when a hook fails, including how to fix the problem and how to bypass it if that is allowed.
- Add a setup step that cannot be missed, such as a
make setuptarget or a script that runsgit config core.hooksPath .githooks. - Do not use hooks to enforce secrets scanning alone. Secrets that reach the remote are exposed even if CI later fails, so run a scan in CI too, and rotate any secret that was pushed.
Quick self-check for a repository
Run these commands in a clone to see whether hooks will fire:
git config --show-origin --get core.hooksPath
ls -l .githooks
git ls-files --stage .githooksCheck three things in the output. The core.hooksPath value points to the directory you expect. The hook files have the executable bit. The tracked mode is 100755.
Exercise
A contributor pushes a commit that contains a trailing-whitespace error, and your pre-commit hook was supposed to block it. Name three different reasons the commit got through, and say which single control would still have stopped the change from reaching the main branch.
Show answer
Any three of: the contributor used git commit --no-verify; the hook was never installed in their clone (for example core.hooksPath was not set); the hook file was not executable so Git ignored it; the commit was made by a rebase, an IDE, or a web editor that does not run the hook; the hook checked the working tree instead of the staged content; the hook failed open when a tool was missing. A required CI status check on a protected main branch, which runs the same check on a clean checkout, would have blocked the merge.
Exercise
Your hook runs npm test and takes four minutes, and developers have started using --no-verify on every commit. What change would you make to the hook and to the CI configuration?
Show answer
Move the full test suite out of pre-commit. Keep only fast checks (lint on staged files) in the hook, optionally run heavier tests in pre-push, and run the complete suite as a required CI status check. This keeps the local feedback quick so people stop bypassing it, and CI still enforces the full policy.