Home / Knowledge hub / Pre-commit hook
Knowledge hub
Pre-commit hook
A small check that runs on a developer's machine before code is committed.
What it means
A pre-commit hook is a script that Git runs automatically just before it records a commit. If the script exits with an error, the commit is stopped. It lives on the developer's own computer, inside the repository's hooks folder, and runs in seconds.
Teams use it to catch cheap problems early: a stray password, a formatting slip, a banned file type. Because it runs before the code ever reaches a shared repository, a mistake is fixed in private instead of being published to the whole team.
Why it matters
The most useful job is stopping secrets from entering history. Once a key is pushed, it must be treated as exposed even if you delete it later, because it stays in the repository's history and in every clone. Stopping it before the commit avoids a rotation.
It also makes good habits the default. Developers get instant feedback on the line they just wrote, which teaches faster than a report weeks later. That supports the idea of shifting left, finding problems where they are cheapest to fix.
How it shows up
Hooks are usually managed with a shared configuration file, so everyone runs the same checks. Typical ones scan for secrets, run a linter, check for large files and block commits straight to the main branch. Keep them fast, or developers will switch them off and the checks will protect nobody.
The key limit is that a hook is optional. Hooks are not copied when someone clones a repository, and a developer can skip them with a flag. So never treat a pre-commit hook as the only control. Repeat the important checks on the server, in the pipeline, where they cannot be bypassed.
Questions people ask
Pre-commit hook, answered
Can developers bypass a pre-commit hook?
Yes. Git lets a developer skip hooks with a flag such as --no-verify, and hooks do not install themselves on a fresh clone. That is why the same checks should also run on the server.
What is the difference between pre-commit and pre-push hooks?
A pre-commit hook runs when you create a commit. A pre-push hook runs when you send commits to a remote. Pre-push can run slower checks, because it happens less often.
Should a pre-commit hook run a full security scan?
Usually not. Hooks need to finish in seconds. Run quick checks locally, such as secret detection, and leave heavy analysis to the pipeline, where time matters less. Developers will otherwise skip the hook to save time.
Does a pre-commit hook replace CI checks?
No. It is a convenience for the developer, not a gate. The pipeline is where you enforce rules for everyone, because nobody can quietly opt out of it. Treat it as a first line, not the last.
How do we get the whole team to use the same hooks?
Keep the hook configuration in the repository and add a one-line setup step to the project's onboarding instructions. A server-side check then catches anyone who forgets. This way a new joiner gets the same checks as everyone else.
Sources
Where this comes from
Related
Terms that sit next to this one
Secret scanning
Searching code and history for keys, passwords and tokens that should not be there.
Shift left
Doing security work while code is written, not as a check at the end.
Hardcoded secret
A password, API key or token written straight into source code.
Branch protection
Repository rules that stop unreviewed code reaching the main branch.
Pull request security check
Automatic security testing that runs on a code change before it is merged.
What if the hook gets skipped?
A hook only catches mistakes you thought of. We test the running app for what slipped through, and in CI/CD we can run on a schedule or gate a build. Ask us to run it on one of your applications.
Or start with a $199 pilot on one application: thirty days, success criteria agreed before day one, credited against the annual if you convert.