Stop Secrets Before They Reach Git: A Pre-Commit Setup
Secret scanning with a pre-commit hook checks staged changes for API keys and passwords before a commit is created, so the leak never enters git history. Pair it with a CI check and server-side push protection, because local hooks can be skipped.
Here's how to set up all three layers, tune out false positives, and respond when a real credential slips through.
Vendors Covered in this Article
Disclosure: We may earn a commission if you buy through some links on this page. It doesn't change what we recommend.
Why do you need three layers, not one?
Each layer catches what the previous one misses:
- Pre-commit hook: fastest feedback, runs on the developer's machine, scans only staged changes. Anyone can bypass it with a flag, or may never have installed it.
- CI check: runs on every pull request in a controlled environment, and can't be skipped without changing the pipeline. Slower to give feedback, but reliable.
- Push protection on the server: the git host blocks a push containing a recognized secret pattern. GitHub Advanced Security offers this for supported patterns, which makes it a useful backstop for people who skip hooks.
If you only do one, do the server-side or CI layer, since it doesn't depend on every laptop being configured. The hook is what makes the experience pleasant.
How to set up the pre-commit hook
Use a hook manager that keeps configuration in the repository, so everyone gets the same behavior.
- Choose an open-source secret scanner that supports staged-file scanning and custom rules.
- Add a pre-commit configuration file to the repository that runs the scanner against staged changes only, so it stays fast.
- Document the one-line install command in your README and in the new-hire setup checklist.
- Add a check to onboarding that confirms the hook is installed, for example a script that fails the first build if it isn't.
- Test it by staging a fake key in a scratch branch and confirming the commit is rejected.
Keep execution time low. If the hook takes several seconds, people will disable it. Scanning only what changed, and skipping lockfiles and generated files, keeps it quick.
How do you handle false positives without weakening the rules?
Noisy scanners get turned off, so plan for tuning from day one. Test fixtures, example values in documentation and high-entropy hashes will trigger detections.
Add allowlist entries narrowly: a specific file path and pattern, not an entire directory of code. Prefer a visible inline annotation on the line, with a comment saying why, so reviewers see it. Review the allowlist in pull requests like code. If a fixture must look like a key, make it obviously fake, such as a string that spells out 'example'. Finally, run a one-time scan of existing history to establish a baseline and clean up old findings separately, so the hook only judges new changes.
What do you do when a real secret leaks?
Assume any secret that reached a remote repository is compromised, even in a private one and even if you delete it quickly. Rewriting history doesn't help with clones, forks and caches.
- Revoke or rotate the credential first, before anything else.
- Check the provider's logs for use of the key since it was committed.
- Remove it from the code and, if you choose, from history, after rotation.
- Replace the hard-coded value with a reference to a secrets store or environment injection.
- Note the incident, and decide whether it needs formal handling under your incident process.
If the secret gave access to customer data, involve your attorney about notification duties.
Where should the secrets live instead?
Scanning catches mistakes, but the better fix is fewer places for secrets to exist. Move credentials into a secrets manager and inject them at deploy or run time, so developers rarely handle raw values. A tool like Doppler syncs secrets to each environment and cuts the sprawl of local .env files, while AWS-native teams often use AWS Secrets Manager; the tradeoffs are in HashiCorp Vault vs AWS Secrets Manager vs Doppler and the cost side in the AWS Secrets Manager cost calculator. Add dependency checks in the same pipeline with dependency vulnerability scanning on GitHub.
What Good Looks Like
Secrets are blocked at commit, at pull request and at push, and any leaked credential is rotated within the hour.
Building The Capability (5-Stage Skill Ladder)
How to Get Started
Disclosure: We may earn a commission if you buy through some links on this page. It doesn't change what we recommend.
Fits when you want push protection on the server that blocks recognized secret patterns for everyone, hooks or not.
Fits when the goal is to stop keeping secrets in local files and sync them to each environment from one place.
Frequently Asked Questions
What is a pre-commit hook for secret scanning?
It's a script git runs before creating a commit. It scans staged changes for patterns that look like API keys or passwords and rejects the commit if it finds one, keeping secrets out of history.
Can developers bypass a pre-commit hook?
Yes, with a flag or by never installing it. That's why you also need a CI check and push protection on the server, which don't depend on individual machines.
Is deleting a leaked secret from git history enough?
No. Copies may exist in clones, forks and caches, so rotate or revoke the credential first. Removing it from history is cleanup, not remediation.
How do you reduce false positives?
Allowlist narrowly by file path and pattern, annotate exceptions inline with a reason, make test values obviously fake, and skip lockfiles. Review the allowlist in pull requests just like code.
About the numbers
This guide doesn't quote a sourced benchmark. Figures in it are estimates or general guidance, so check them against your own numbers.
Related Guides
HashiCorp Vault vs AWS Secrets Manager vs Doppler: Secrets Platforms
Compare Vault, AWS Secrets Manager, and Doppler for secret sprawl prevention, dynamic credential rotation, Kubernetes injection, and SOC 2 audits.
Estimating AWS Secrets Manager Cost: A Worksheet
A worksheet to estimate AWS Secrets Manager cost from secret count, API calls, rotation and encryption keys, and to compare against Doppler.
Set Up Dependency Vulnerability Scanning on GitHub
Set up dependency vulnerability scanning on GitHub: turn on alerts and update PRs, add a CI gate, triage findings by reachability, and set fix deadlines.
Doppler or AWS Secrets Manager for a Multi-Cloud SaaS Stack
A criteria-based way for B2B SaaS teams to pick between Doppler and AWS Secrets Manager, based on where your deploys actually run and who audits you.
Snyk vs Veracode vs GitHub Advanced Security: AppSec Tool Comparison
Compare Snyk, Veracode, and GitHub Advanced Security: SAST, SCA, container security, secret scanning, automated remediation, and SOC 2 compliance.
Application Security Tooling for Multi-Tenant B2B SaaS
A decision framework for choosing Snyk or GitHub Advanced Security when your B2B SaaS product runs on shared, multi-tenant infrastructure.