12k
All articles

Setting Up Gitleaks as a Pre-Commit Hook

Set up the Gitleaks pre-commit hook to scan staged changes, block commits containing secrets, handle false positives, and add a CI scan as a backstop.

OpenReplay Team
OpenReplay Team
Setting Up Gitleaks as a Pre-Commit Hook

The Gitleaks pre-commit hook scans the changes you have staged and, when it finds a secret, fails so Git aborts the commit before anything reaches history.

Removing a leaked key after a push means rotating it and rewriting history. AI coding tools make that more likely: GitGuardian’s State of Secrets Sprawl 2026 found that AI-assisted commits leaked secrets at roughly twice the GitHub-wide baseline. Blocking the key at commit time costs one YAML block.

This guide covers the current configuration, a test with a fake key that the hook really catches, how to read a blocked commit, and what to do with false positives and emergencies. Many gists and posts still call gitleaks protect --staged. This guide does not.

Key Takeaways

  • The gitleaks pre-commit hook runs gitleaks git --pre-commit --redact --staged --verbose, so it scans only staged changes and masks secret values in its output.
  • gitleaks git is the command for scanning repositories. detect and protect have been deprecated since v8.19.0.
  • Gitleaks allowlists AWS access keys that end in EXAMPLE, so test the hook with a different fake token.
  • Use gitleaks:allow for a single line, a Fingerprint in .gitleaksignore for a single finding, and .gitleaks.toml for paths or patterns across the repo.
  • SKIP=gitleaks git commit skips the hook for one commit, so run the same scan in CI as a backstop.

What Does the Gitleaks Pre-Commit Hook Do?

The Gitleaks pre-commit hook runs every time you commit. Gitleaks reads the staged diff, compares it against its rule set and returns a non-zero exit code when it finds something. pre-commit treats that exit code as a failure and stops the commit. If the pre-commit framework is new to you, read automating code checks with Git pre-commit hooks first.

The Gitleaks README says the project is feature complete and that future releases will contain security patches only.

Install Gitleaks and pre-commit

You don’t need to install the Gitleaks CLI for the hook to work. The gitleaks hook id is a Go hook, and pre-commit 3.0.0 and later downloads Go itself and builds the hook when Go isn’t installed. Install the CLI anyway, because you need it for manual scans and for CI:

# Homebrew
brew install gitleaks

# Docker
docker pull ghcr.io/gitleaks/gitleaks:latest

Prebuilt binaries are on the Gitleaks releases page. To install pre-commit itself, follow the pre-commit installation docs.

Add the Gitleaks Hook to .pre-commit-config.yaml

To set up the Gitleaks pre-commit hook, add the Gitleaks repo to .pre-commit-config.yaml, pin a release tag, refresh the pin and install the Git shim:

repos:
  - repo: https://github.com/gitleaks/gitleaks
    rev: v8.30.1
    hooks:
      - id: gitleaks
pre-commit autoupdate
pre-commit install

pre-commit install writes the shim to .git/hooks/pre-commit. If you want the official Docker image instead of a local Go build, change id: gitleaks to id: gitleaks-docker.

Read the output of pre-commit autoupdate before you accept it. The v8.30.1 tag points to a commit that isn’t reachable from the master branch, so, as Gitleaks issue #2058 reports, pre-commit autoupdate downgrades v8.30.1 to v8.30.0. If autoupdate proposes v8.30.0, set the pin back to v8.30.1 by hand.

According to .pre-commit-hooks.yaml, the hook runs gitleaks git --pre-commit --redact --staged --verbose with pass_filenames: false. If an older bare hook script still calls a deprecated command, delete it:

#!/bin/sh
# .git/hooks/pre-commit — deprecated, replace with the framework config above
gitleaks protect --staged

Secret scanning works best alongside a correct ignore file. Keep .env out of Git with a .gitignore generator.

Run the Hook Once, Then Commit a Fake Key

Run the hook by hand once. This confirms that the hook environment builds and runs:

pre-commit run gitleaks --all-files

The first run takes longer because pre-commit compiles Gitleaks. The hook hard-codes --staged and doesn’t accept filenames, so --all-files does not scan the whole repository. It still checks only the staged diff.

Next, stage a token that Gitleaks detects. The README’s sample output reports this sidekiq credential under RuleID sidekiq-secret:

echo 'export BUNDLE_ENTERPRISE__CONTRIBSYS__COM=cafebabe:deadbeef' > demo.env
git add demo.env
git commit -m "test gitleaks hook"
echo $?

Don’t test with AWS’s documentation keys. Gitleaks allowlists AWS access keys that end in EXAMPLE, the placeholder format AWS uses in its own docs, so a commit containing one passes the hook without a finding.

How Do You Read a Blocked Commit?

When the Gitleaks hook finds a secret, pre-commit marks it Failed, Gitleaks prints one block per finding, and the commit is not created. The skeleton below shows the shape of the output. The values in angle brackets are placeholders:

Detect hardcoded secrets.................................................Failed

Finding:     export BUNDLE_ENTERPRISE__CONTRIBSYS__COM=REDACTED
Secret:      REDACTED
RuleID:      sidekiq-secret
File:        demo.env
Line:        1
Fingerprint: <value to copy into .gitleaksignore>
FieldWhat it tells you
RuleIDThe rule that matched
File / LineWhere the secret is
FingerprintA stable ID for this finding, used by .gitleaksignore

Gitleaks masks the secret value because the hook passes --redact. The README’s exit code table lists exit code 1 for leaks or an error, and that is why Git aborts. An aborted commit is the cheapest outcome, because a key that reaches a pushed commit has to be rotated. If a secret is already in history, follow the steps in what to do when your API keys end up in a repo.

How Do You Handle Gitleaks False Positives?

Gitleaks gives you three ways to suppress a false positive. Pick the narrowest one that fits.

ToolScopeLives inUse when
gitleaks:allowOne lineThe source fileA test value sits inline in code
.gitleaksignoreOne findingRepo root, by FingerprintYou can’t edit the line, or it’s a one-off
.gitleaks.tomlPaths or patterns, repo-wideRepo rootA whole class of files is noise

Inline comment:

TEST_TOKEN = "not-a-real-token"  # gitleaks:allow

.gitleaksignore takes one Fingerprint per line, copied from the finding:

<Fingerprint from the blocked commit>

.gitleaks.toml extends the default rules and adds a path allowlist:

[extend]
useDefault = true

[[allowlists]]
description = "test fixtures"
paths = ['''^tests/fixtures/''']

Keep useDefault = true as the README’s configuration section shows it. Anyone reviewing the PR can see every entry in .gitleaks.toml and .gitleaksignore, so changes to them get checked like any other code change.

Skip the Hook Once, but Scan in CI

SKIP=gitleaks git commit skips the Gitleaks hook for that commit and still runs the rest of your hooks:

SKIP=gitleaks git commit -m "emergency: skip gitleaks"

A pre-commit hook runs on the developer’s machine and can be skipped, so run the same scan in CI. The --ignore-gitleaks-allow flag makes that scan disregard inline gitleaks:allow comments:

gitleaks git -v --ignore-gitleaks-allow

On GitHub, gitleaks-action wraps the same scanner. To audit the full history of an existing repo, see scanning your repo for secrets with TruffleHog.

Conclusion

Setting up Gitleaks as a pre-commit hook takes three steps: pin v8.30.1 in .pre-commit-config.yaml, check what pre-commit autoupdate proposes, and run pre-commit install. After that, every staged secret blocks the commit with a finding you can act on. Commit the demo.env file now to confirm the block works, then add gitleaks git to your CI pipeline so skipped commits still get scanned.

FAQs

What is the difference between SKIP=gitleaks and git commit --no-verify?

SKIP=gitleaks turns off only the hook with the id gitleaks, and every other hook in .pre-commit-config.yaml still runs. git commit --no-verify, or its short form -n, makes Git bypass the pre-commit and commit-msg hooks completely, so linters, formatters and every other framework hook are skipped as well. Neither one scans the commit, so a Gitleaks scan in CI is the check that developers cannot bypass.

Why does the gitleaks-system hook fail on every commit or pass without scanning?

In v8.30.1, the gitleaks-system hook definition does not set pass_filenames: false, but the gitleaks and gitleaks-docker definitions do. Because of this, pre-commit adds the staged filenames as arguments to gitleaks git, which accepts at most one argument. If two or more files are staged, the hook errors. If exactly one is staged, nothing gets scanned and the hook passes. To fix it, add pass_filenames: false under the gitleaks-system id.

Does Gitleaks check whether a detected secret is still active?

No. Gitleaks matches content against regex rules and uses Shannon entropy as a secondary signal. It runs entirely offline, so a finding means a string looks like a secret, not that the credential works. TruffleHog adds live verification and calls the provider's API to confirm a credential is valid. Skipping that network step makes Gitleaks fast enough for a pre-commit hook. Verified scans fit better in CI or scheduled history audits.

Understand every bug

Uncover frustrations, understand bugs and fix slowdowns like never before with OpenReplay — self-hosted, with full data ownership.

Star on GitHub

We use cookies to improve your experience. By using our site, you accept cookies.