Explore our new Cybersecurity Terms Glossary, explained by our experts.
Cybersecurity Terms Glossary
Pre-commit security hooks run checks on your machine before a commit is created, catching secrets, private keys, and obvious misconfigurations at the cheapest possible moment to fix them.

What Are Pre-Commit Security Hooks?
A pre-commit hook is a script git runs on your machine before it creates a commit. If the script exits non-zero, the commit does not happen. Security hooks use that moment to look for the things that are trivial to stop now and expensive to deal with later: a credential pasted into a configuration file, a private key, an enormous binary, a Terraform file that opens a security group to the world.
The appeal is arithmetic. A secret caught before git commit completes costs a developer thirty seconds. The same secret pushed to a shared remote costs a credential rotation, a log review, and possibly a breach assessment.
Native git hooks live in .git/hooks, which is not part of the repository. They are not cloned, not reviewed, and not updated, so in practice they exist on one person’s laptop and nowhere else.
The pre-commit framework fixes this. Configuration lives in a committed file, each developer runs pre-commit install once, and the tool downloads and pins the hooks itself:
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.21.2
hooks:
- id: gitleaks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: detect-private-key
- id: check-added-large-files
args: ["--maxkb=1024"]
- repo: https://github.com/bridgecrewio/checkov
rev: 3.2.0
hooks:
- id: checkov
args: ["--quiet", "--compact"]
Pinning the rev matters for more than reproducibility. An unpinned hook repository is remote code that runs on every developer machine, which is an attractive target; pin it, and update deliberately.
The constraint is time. A hook suite that adds more than a few seconds gets disabled, so choose checks that are fast and almost never wrong.
Run everything heavier in continuous integration. A hook that triggers a full static analysis pass is a hook that will be bypassed by lunchtime.
This is the point most often missed. git commit --no-verify skips every hook. So does committing from a web interface, a graphical client that does not run hooks, or a machine where nobody ran the install step. Pre-commit hooks help people who want to do the right thing; they stop nobody who does not.
Real enforcement needs two further layers:
Branch protection rules that require those checks to pass before merge complete the picture. The local hook, the pipeline check, and the platform block are the same rule applied three times, which is the right amount for something this cheap to get wrong.
Introduce the suite in a repository where someone will champion it, start with secret detection alone, and run pre-commit run --all-files once to find and clear the existing debt before you enable the hook. Add a baseline for findings you have assessed and cannot fix immediately, and shrink it deliberately. Put the installation step in the project’s setup script so new joiners get it without reading anything.
Then watch for bypass. If people are routinely using --no-verify, the suite is too slow or too noisy, and the fix is to trim it rather than to send a reminder about policy. A hook that developers respect is worth more than a comprehensive one they route around, and it supports the broader DevSecOps idea that checks belong where the work happens.
Yes, trivially, with the no-verify flag on git commit. They can also commit through a web interface or a client that does not invoke hooks, or simply never run the install step. Treat hooks as a helpful early warning and put the enforcing version of the same check in continuous integration and in platform push protection, where it cannot be skipped by the person making the commit.
Secret detection, private key detection, and a large file size limit. Those three take about a second, almost never produce a false positive, and prevent the mistakes that are hardest to undo once they reach a shared repository. Add infrastructure as code scanning if the repository contains Terraform or Kubernetes manifests. Resist adding anything that takes longer than a few seconds.
Run the scanner across all files before enabling the hook, so you know what is there. Rotate anything genuinely live, because the credential is compromised regardless of what you do to the history. Record assessed but unfixable findings in a baseline file so the hook only fails on new issues, and plan to shrink that baseline rather than letting it become permanent.
Not strictly, but hand-written hooks in the git hooks directory are not cloned with the repository, so they end up installed on one machine and forgotten. The framework keeps the configuration in version control, pins hook versions, manages the tool environments, and gives everyone the same checks. Pin the revisions you depend on, since unpinned hooks are remote code running on every developer’s machine.
You May Be Interested In These
We use a few cookies that are needed to make this website work, and they are always on. We would also like to set analytics cookies, to see which pages are useful; functional cookies, so that our live chat works; and advertising cookies, to measure our campaigns. We will not set any of those until you say yes, and you can change your mind at any time from the Cookie Settings link in the footer.
Choose the cookies you are happy for us to use. Strictly necessary cookies are always on; everything else is off until you turn it on.
These cookies keep the website working: they hold your security checks, your form submissions and the choice you make here. The site cannot run without them, so they are always on.
These let our live chat open and remember your conversation, and let the newsletter sign-up work. Switch them off and the chat window will not appear.
These tell us which pages are read and how people find us, so that we can improve the site. The reports are about groups of visitors, never about you personally.
These let us measure our advertising and show you our adverts on other websites, such as LinkedIn, Google and Meta. Switch them off and you will still see adverts, but they will be less relevant.