Join our Newsletter — 33% off our NHI Course
Home› Guides› Pre-Commit Hooks for Beginners: Catching Secrets and Mistakes…
Guide Secrets Management

Pre-Commit Hooks for Beginners: Catching Secrets and Mistakes Before Code Leaves Your Laptop

PDF for newsletter subscribers · free to read online

← All guides
By NHI Mgmt Group, under the editorial direction of Lalit Choda Updated 30 November 2026 9 min read
On this page

A pre-commit hook is a small check that runs on your computer every time you save code with Git.

This guide is for new developers, students and team leads who want a simple safety net. You need to know what Git is, but nothing more. The hook can stop a password or API key before it enters your project history.

In short

  • A pre-commit hook runs just before Git saves a commit, and can stop it if something looks wrong.
  • Teams use them to catch secrets such as API keys, plus tidy-up problems like stray spaces.
  • Hooks live on your own machine, so anyone can skip them. Back them up with a check on the server too.
  • If a secret was already pushed, deleting the line is not enough. The secret must be replaced.

What you will learn

  • Explain what a pre-commit hook is and when it runs.
  • Describe how a secret-scanning hook stops a leak.
  • Know why hooks are a helper, not a full control.
  • Set up a first hook with your team.
Saving a change (commit)
a snapshot of your code that Git records in the project history.
Automatic check (hook)
a small program Git runs by itself at set moments.
Hidden password (secret)
a password, key or token that lets software into another system.

What is a pre-commit hook?

Think of a spell checker that runs before an email is sent. If it finds a problem, the email waits until you fix it. A pre-commit hook does that job for code.

Git can run small programs, called hooks, at set points in its work [1]. The pre-commit hook runs when you type git commit, before the commit is made. If the hook reports a failure, Git stops the commit [1].

Hooks sit in a folder inside your project, called .git/hooks [1]. Many teams use a free tool, also called pre-commit, to install and share the same hooks across the team [2].

Where the comparison stops working: you cannot easily turn off someone else's spell checker. Anyone can skip a hook on their own machine, so it is a helper and not a lock.

Why does it matter?

Once a secret is in Git history, it can travel with every copy of the project. Catching it before the commit is the cheapest fix there is.

If you are a new developer

What could happen: you paste a test key into code to get something working, then forget it is there.

Check this: ask whether your team's projects run a secret-scanning hook.

Next: Git Secret Scanning Guide

If you lead a small team

What could happen: each person sets up different checks, or none, so mistakes slip through.

Check this: keep one shared hook settings file in each project.

Next: Git Hooks and Pre-commit Security Guide

If you look after security

What could happen: you rely on hooks alone, and a skipped hook lets a key reach the shared server.

Check this: run the same scan on the server as well.

Next: How to enable GitHub secret scanning push protection

What does it look like?

Here is a made-up settings file for the pre-commit tool. It tells the tool which checks to run.

Example only. All values are made up.

# .pre-commit-config.yaml (EXAMPLE)
repos:
  - repo: https://example.com/secret-scanner   # (1)
    rev: v0.0.0-FAKE                           # (2)
    hooks:
      - id: scan-for-secrets                   # (3)
  - repo: https://example.com/tidy-checks      # (4)
    rev: v0.0.0-FAKE
    hooks:
      - id: trailing-whitespace                # (5)
  1. Where the check comes from. In a real file, this line would contain the address of a known scanner project.
  2. The exact version to use. A fixed version means everyone runs the same check.
  3. The check that looks for passwords, keys and tokens.
  4. A second set of checks, for tidy-up jobs.
  5. A simple check that removes stray spaces at the ends of lines.

Why this is risky: without a settings file, each developer runs different checks, or none at all.

The safer way: keep this file in the project, pin each version, and ask everyone to switch it on.

How does it work?

  1. You save some work. You type git commit as normal.
  2. Git runs the hook. Before it records anything, Git starts the pre-commit hook [1].
  3. The hook checks your changes. The pre-commit tool runs each check only on the changes you are about to save [2].
  4. A check fails. If a scanner such as Gitleaks spots something that looks like a key, the hook reports a failure [3].
  5. Git stops the commit. Nothing is saved, and you see a message. You remove the secret and try again [1].
  6. The server checks again. When you push, a service such as GitHub push protection can block a known secret a second time [4].
Diagram titled Where a pre-commit hook catches a secret. Four boxes in a row, linked by arrows: You type git commit; The hook scans your staged changes; Secret found, so the commit is stopped and you fix it; Clean commit is saved and pushed. A note below says hooks run on your own machine and can be skipped, so a second scan on the server is the backup.
Figure 1. The hook checks your changes before Git saves them, and the server checks again on push.

Git lets anyone skip the hook with one option [1]. That is why a second check on the server matters.

A worked example

This story is made up to show the idea.

Zane Ulrich is a junior developer on a small web team. He is building a feature that sends text messages through an outside service.

To test quickly, Zane pastes the service's API key straight into his code. It works, so he types git commit.

The commit does not happen. Instead, a message says the secret scanner found something that looks like a key, with the file name and line.

Zane asks his team lead, Imani Rosen, what to do. She set up the hook last month, with one shared settings file for the project.

Imani shows him how to read the key from the app's settings instead of the code. Zane moves the key, and the next commit goes through.

Imani also explains why the check matters. If the key had been pushed, the team would have had to cancel it and get a new one. Deleting the line would not remove it from history.

Finally, she points out the backup. The team's code server also scans every push, in case someone skips the hook.

Zane spent two minutes on the fix. A leaked key would have meant a new key, an update to every system using it and a check of the service's usage logs.

Myths and mistakes

What people thinkWhat is true
A pre-commit hook stops every leak.Anyone can skip a hook on their own machine [1]. Keep a server check as well.
Hooks come with the project when you copy it.Each person usually switches them on, for example by running pre-commit install [2].
If a secret was committed, deleting the line fixes it.It stays in history. Replace the secret, then clean up the history.
Skipping the hook is fine when you are in a hurry.Skips should be rare and explained. A test value can be marked as allowed instead [3].
The latest version of each check is always best.Pin a fixed version, so the whole team runs the same check [2].

What can I do this week?

  • Check whether your main project has a shared hook settings file.
  • Add a secret-scanning check, then run it once over all files.
  • Ask whether your code server also scans pushes for secrets.
  • Agree with your team when it is acceptable to skip a hook.

Checklist

  • I know what a pre-commit hook is and when it runs.
  • My project has a shared settings file for its hooks.
  • A secret-scanning check runs before each commit.
  • Each check is pinned to a fixed version.
  • I have switched the hooks on for my own copy of the project.
  • Our code server scans for secrets as a backup.
  • I know that a pushed secret must be replaced, not just deleted.

Common questions

Do pre-commit hooks slow me down?

Usually only a little. The pre-commit tool checks only the changes you are about to save, not the whole project. A slow check can run on the server instead. Most teams find that a few seconds per commit is a fair price.

What happens if the hook finds a secret?

Git stops the commit and shows a message. Nothing has been saved yet, so you can remove the secret and try again. Move the value into the app's settings or a secrets manager instead of the code.

What if the scanner flags a fake test value?

That is a false alarm. Many scanners let you mark one line as allowed. Gitleaks, for example, accepts a special comment on that line. Use this rarely, and only for values you know are fake.

Is a pre-commit hook enough on its own?

No. It runs on each person's machine, and anyone can skip it. Pair it with a scan on the code server, such as push protection, and with a plan for replacing any secret that does leak.

Can a hook be dangerous?

Yes. A hook is a program that runs on your computer. Only use hooks from projects you trust, and pin them to a fixed version. Our practitioner guide covers this risk in more detail.

Where next?

Ready for the detail? Read the practitioner guide: Git Hooks and Pre-commit Security Guide. It covers hook design, bypass handling and the risk of running untrusted hooks.

Also useful:

Related NHI Mgmt Group resources: Git Repository Security Guide, Developer Secret Scanning Triage and Environment Variables and .env Files.

Key terms

Pre-commit hook
An automated local check that runs before code is written into Git history.
Git hook
A local script that Git runs automatically when an event happens, such as a commit.
Commit
A recorded snapshot of changes in a Git repository.
API key
A code that lets one piece of software prove itself to another service's API.
Hardcoded secret
A credential written directly into code, scripts or configuration files.
Push protection
A server-side check that blocks some secrets when code is pushed.

Sources and further reading

  1. Git, "githooks: Hooks used by Git", Git documentation. https://git-scm.com/docs/githooks (accessed 30 Nov 2026).
  2. pre-commit, "pre-commit: A framework for managing and maintaining multi-language pre-commit hooks". https://pre-commit.com/ (accessed 30 Nov 2026).
  3. Gitleaks, "Gitleaks" project README, GitHub. https://github.com/gitleaks/gitleaks (accessed 30 Nov 2026).
  4. GitHub, "About push protection", GitHub Docs. https://docs.github.com/en/code-security/secret-scanning/introduction/about-push-protection (accessed 30 Nov 2026).

PDF for subscribers

Take this guide with you

Every section, table and checklist in one PDF, with links to the sources and related guides. Subscribe to our weekly NHI & AI Identity Journal and we’ll email you a download link.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

PDF Downloads for Newsletter Subscribers

Get the guide

Enter your details and we’ll email you a download link.

The full guide is free to read online. The PDF is a thank you for subscribing.

Fill in all fields marked * and tick both boxes to continue.