Add the official Gitleaks pre-commit hook, and every commit has its staged changes scanned before the commit exists [2]. Pair it with TruffleHog when you need to know whether a detected credential still works, because TruffleHog tests detections against the issuing service [3]. To remove leaked secrets from git history, rotate the credential first, then rewrite history with git-filter-repo only when rotation leaves risk behind [5].
- The Gitleaks hook scans staged changes only, so a commit-time check stays narrow [2].
- A local hook protects only the clones that installed it, and one flag skips it for a single commit [1][4].
- TruffleHog classifies over 800 secret types and can test whether each detected secret is live [3].
- The Gitleaks README says the tool is feature complete and future releases are security patches only [1].
- Rotation comes first, and GitHub says it may remove the need to rewrite history [5].
- A force push alone leaves old commits reachable in forks, clones, cached views and pull requests [5].
Secret scanning is automated detection of credentials such as API keys, tokens and private keys inside source code and git history. A scanner matches patterns or entropy in file contents, and some tools also test whether a match still authenticates. Gitleaks and TruffleHog are two open source scanners built for git repositories.
Gitleaks is an open source scanner, released under the MIT license, that looks for secrets such as API keys and tokens in git repositories, directories and standard input [1]. Its git command reads a repository's history through the output of git log -p, so it inspects the patches that each commit added [1]. Its dir command scans files and folders without needing git history, and its stdin command accepts streamed data [1]. Detection rules live in a TOML configuration that combines Go regular expressions with optional entropy thresholds and keyword prefilters [1]. A scan exits with code 1 when it finds a leak, and a flag can change that code [1]. For repositories with long histories, an earlier report can act as a baseline, so later scans show only new findings [1]. For pre-commit use, the project ships hook definitions that scan staged changes [2].
How Does a Gitleaks Pre-Commit Hook Setup Work?
A Gitleaks pre-commit hook scans your staged changes each time you run git commit, and a match blocks the commit. The pre-commit framework manages the hook through one YAML file in the repository root. Setup takes five actions: install the framework, add the config, update the pinned version, register the hook with git, and test it with a fake credential.
Gitleaks publishes its hook definitions in a .pre-commit-hooks.yaml file at the root of its repository. The default hook id, gitleaks, runs gitleaks git --pre-commit --redact --staged --verbose, so Gitleaks reads staged changes and masks any secret it prints [2]. Two sibling ids, gitleaks-docker and gitleaks-system, run the same command from a Docker image or from a binary already installed on the machine [2].
- Install the pre-commit framework from pre-commit.com [1].
- Create
.pre-commit-config.yamlin the repository root with the block below [1]. - Run
pre-commit autoupdateso the rev value moves to a current Gitleaks release [1]. - Run
pre-commit installto register the hook with git [1]. - Stage a file that holds a made-up test credential and run git commit. The hook should report Failed [1].
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.24.2
hooks:
- id: gitleaks
The README example pins rev v8.24.2, a number that ages, so run the autoupdate step instead of copying it [1]. Teams that skip the framework can copy the example pre-commit.py script into .git/hooks/ instead [1].
Gitleaks gives you three ways to quiet a finding, and each deserves a glance in code review. A #gitleaks:allow comment on the offending line tells Gitleaks to ignore it, and a .gitleaksignore file lists the fingerprints of findings to skip, a feature the README calls experimental [1]. A baseline report, created with --report-path and applied with --baseline-path, hides old findings so only new ones appear [1].
Skipping the hook is just as easy. Prefixing a commit with SKIP=gitleaks bypasses the Gitleaks hook for that run [1], and git commit --no-verify skips git hooks in general [4]. GitHub's guidance tells maintainers to ask each collaborator to set up whichever hook the team chose [5].
A hook protects the clones that installed it. CI protects the repository.
TruffleHog vs Gitleaks: Which Scanner Fits Your Workflow?
Gitleaks matches secrets with configurable regex and entropy rules, while TruffleHog classifies over 800 secret types and can check whether each one still authenticates. Gitleaks suits fast commit-time gates and custom rules, and TruffleHog suits triage, where a verified result points to a live credential. A sensible split is Gitleaks on the developer machine and TruffleHog in CI or audits.
This table compares Gitleaks and TruffleHog on the points that decide tool choice, using each project's own README and hook files for every cell.
| Question | Gitleaks | TruffleHog |
|---|---|---|
| License | MIT [1] | AGPL-3.0 [3] |
| How it detects | Regex rules with optional entropy and keyword prefilters, set in TOML [1] | Detectors that classify over 800 secret types [3] |
| Checks whether a secret still works | The README describes no verification step [1] | Yes. Results are verified, unverified or unknown [3] |
| What it scans | Git repositories, directories and stdin [1] | Git, GitHub, GitLab, Docker images, S3, GCS, filesystems and stdin, among others [3] |
| Pre-commit support | Official hook ids that scan staged changes [2] | Documented setups for the pre-commit framework, Husky and a global core.hooksPath [4] |
| Ignoring a known value | #gitleaks:allow comment, .gitleaksignore file, baseline report [1] | trufflehog:ignore comment [3] |
| Exit code when it finds something | 1 by default [1] | 183 when the --fail flag is set [3] |
| Project direction | Maintainer says feature complete, security patches only [1] | README documents SARIF output and Hugging Face scanning [3] |
As of November 13, 2026, the Gitleaks README carries a warning that Gitleaks is feature complete, that future releases will be security patches only, and that the maintainer is shifting focus to a project called Betterleaks [1]. The README does not say whether new detection rules count as features, so review recent commits before relying on Gitleaks for a recently launched token format. The status note is a maintenance signal, and it does not change what Gitleaks detects today.
TruffleHog earns its verified label with a live request. Its AWS detector, for example, calls GetCallerIdentity to see whether a key works [3]. A TruffleHog scan with verification on therefore talks to outside services, and UnpanicTech has not benchmarked how much time that adds to a commit.
TruffleHog's own pre-commit guide covers Husky and a global core.hooksPath setup as well as the pre-commit framework [4]. Setting TRUFFLEHOG_PRE_COMMIT=1 applies its recommended settings, and the manual form is trufflehog git file://. --since-commit HEAD --results=verified,unknown --fail [4]. The guide also warns that git commit -am might bypass the hook for unstaged changes, so stage first and commit second [4].
By default TruffleHog clones a local repository to a temporary directory before scanning, a guard against malicious git configs that its README ties to CVE-2025-41390 [3]. The --trust-local-git-config flag skips that step, and the README says to use it only on repositories you trust [3]. The manual hook configuration includes that flag [4].
A clean result from a verified-only scan does not mean the repository is clean. TruffleHog labels a result unverified when the secret was detected but not confirmed valid, which covers expired values and runs where verification was off [3]. An unverified result still needs a human look.
TruffleHog describes its work in four stages: discovery, classification, validation and analysis [3]. Discovery covers git, chats, wikis, logs, object stores and filesystems [3]. Classification maps a match to one of over 800 secret types, such as an AWS, Stripe or Postgres credential [3]. Validation then logs in with the secret to confirm whether it is live, a step the project says is needed to know whether a present danger exists [3]. Each result carries a status. Verified means an API test confirmed validity, unverified means detected but not confirmed, and unknown means verification failed because of an error such as a network problem [3]. An optional analysis step can show who created a credential and what it can reach, for about twenty commonly leaked credential types [3]. Custom regex detectors are also supported, are marked alpha, and can call a webhook to verify a match [3].
Our analysis, not a benchmark, points to this split:
- Commit time on developer machines: Gitleaks, scanning staged changes only [2].
- Pull request CI: TruffleHog with verification on, so reviewers can separate live credentials from noise [3].
- Legacy repositories: a Gitleaks baseline, so the first run does not bury the team in old findings [1].
- Incident triage: TruffleHog verified results first, unverified results second [3].
Unverified means unreviewed, not harmless.
How Do You Remove Leaked Secrets From Git History?
Removing leaked secrets from git history means rewriting every commit that contains them, which GitHub's documentation does with git-filter-repo. You rotate the credential first, rewrite a local clone, check what changed, force-push the result, and then ask GitHub Support to clear cached views and pull request references while collaborators clean their own clones. A force push alone leaves copies behind.
- Revoke or rotate the exposed credential at the service that issued it [5].
- Install git-filter-repo version 2.47 or newer, the first versions with the
--sensitive-data-removalflag [5]. - Clone the repository and change into its directory [5].
- Run one of the two commands below, then check that the history no longer holds what you meant to remove [5].
- Count the pull requests the rewrite affects with the grep command shown below, because GitHub Support asks for that number [5].
- Force-push with
git push --force --mirror origin. Refs that start with refs/pull/ fail to push by design, and branch protection may need a temporary pause [5]. - Open a ticket with GitHub Support that lists the repository, the affected pull request count and the First Changed Commit(s) from the git-filter-repo output [5].
- Tell collaborators to rebase, not merge, any branch built on the old history [5].
git clone https://github.com/acme-corp/billing-api
cd billing-api
# remove one file from every commit
git-filter-repo --sensitive-data-removal --invert-paths --path config/prod.env
# or replace every string listed in a file kept outside the repo
git-filter-repo --sensitive-data-removal --replace-text ../passwords.txt
# count pull requests touched by the rewrite
grep -c '^refs/pull/.*/head$' .git/filter-repo/changed-refs
GitHub's example gives the full git path to the file, and it adds that a file which once lived at other paths needs an extra --path argument or a second run [5]. The replacement file in GitHub's example sits one level above the repository in ../passwords.txt [5].
GitHub lists side effects that catch teams off guard. Commit hashes change for the commit that introduced the data and every commit after it. Diff views for closed pull requests break, signatures on commits and tags are lost, and any branch protection that blocks force pushes has to be switched off for a while [5].
The sharpest operational detail is recontamination. If a colleague with a pre-rewrite clone runs git pull and then git push, the removed data returns to the repository [5]. Telling every collaborator to discard old clones is part of the fix, not a courtesy.
GitHub's documentation says that when the sensitive data is a credential, the first step is to revoke or rotate it, and that rotation alone may make a history rewrite unnecessary [5]. If a rewrite goes ahead, the work has four parts: rewrite the repository locally with git-filter-repo, update GitHub with the rewritten history, coordinate with colleagues to clean their clones, and prevent repeats [5]. A force push alone leaves the old commits reachable in clones, in forks, in cached views through their SHA-1 hashes, and in pull requests [5]. GitHub Support can remove cached views and pull request references and can run server garbage collection, but only in cases where rotating credentials cannot reduce the risk [5]. Fork owners must clean their own copies, and GitHub cannot provide their contact information [5]. Collaborators should rebase rather than merge branches built on the old history, because one merge commit could bring back some of the tainted history [5].
When Is Rotating the Secret Enough, and When Must You Rewrite History?
Rotating a leaked credential makes the old value useless for access, and GitHub says that may be sufficient on its own. A history rewrite matters more when the leaked data has no rotate button, such as personal records or internal documents. Even then, a rewrite cannot reach copies that other people already hold, so treat the exposure as real until you have checked access logs.
The following reading of GitHub's guidance is our analysis. Rotation handles credentials, and a rewrite handles data that rotation cannot invalidate. GitHub's own wording is narrower: Support helps only where the risk cannot be mitigated by rotating [5].
- Rotation usually settles the case for any credential the issuer can reissue.
- A rewrite plus Support cleanup deserves a serious look for personal data and customer records.
- Access logs for the leaked credential, covering the window between the commit and the rotation, tell you whether anyone used it.
Deleted does not mean gone. TruffleHog ships an alpha feature that enumerates deleted and hidden commits on a GitHub repository and scans them for secrets [3]. GitHub confirms that old commits can stay reachable through cached views by their hashes [5], so assume a removed commit is readable until the Support cleanup is finished.
If the credential reached a production system, open an incident record from your incident response plan template and check whether your data breach notification response plan applies. When the exposed credential belonged to a Microsoft 365 identity, our guide to investigating compromised M365 Entra ID accounts covers the account checks.
Rotate first. Rewrite second.
How Do You Keep Secrets Out of Git After the Cleanup?
Preventing secret leaks in git works in layers: a local hook catches mistakes before a commit exists, a CI scan catches commits made without the hook, and push protection can block pushes that contain recognized secrets. Ignore files for secret-bearing paths, and keep secrets in a manager instead of source code. A Gitleaks baseline lets legacy repositories adopt scanning without a wall of old findings.
- Add secret-bearing files to .gitignore and commit that change so other developers are protected [5].
- Keep secrets out of code, using environment variables or a manager such as Azure Key Vault, AWS Secrets Manager or HashiCorp Vault [5].
- Stage files one by one instead of using git add . or git commit -a, and review the staged diff with git diff --cached [5].
- Turn on push protection for the repository [5].
For a legacy repository, Gitleaks needs two commands to start clean. The first saves a report of everything already in history, and the second scans again while ignoring that report [1].
gitleaks git --report-path gitleaks-report.json
gitleaks git --baseline-path gitleaks-report.json --report-path findings.json
TruffleHog's README shows a GitHub Actions workflow for the CI layer, and that workflow scans the code changes in the referenced commits on pushes to main and on pull requests [3]. The workflow below follows the README and uses fetch-depth 0 so the scan can see history. We would pin the action to a release tag or commit instead of main, which is our preference and not something the README requires.
on:
push:
branches:
- main
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Secret Scanning
uses: trufflesecurity/trufflehog@main
with:
extra_args: --results=verified,unknown
TruffleHog also has a docker command that scans container images [3]. For the cluster side of the same hygiene, see our fixes for Kubernetes container misconfigurations. Package registry tokens are worth adding to any scan scope, and our breakdown of malicious npm and PyPI package mechanics shows what a compromised publishing path can lead to.
What Should Your Team Decide After Reading This?
Secret scanning with Gitleaks and TruffleHog needs three team decisions: which scanner gates commits, which scanner audits history, and who owns the rotate-first runbook. The two tools can share the work, but neither replaces a written response plan. Scanning only pays off when a finding triggers a known action within a known time.
Treat the Gitleaks pre-commit hook as a convenience and the CI scan as the control. A developer can skip the hook with one flag, while a failing pipeline check applies to every pull request. Write that rule down, and a cybersecurity policy for a small business is a natural home for it.
Put a review date on the Gitleaks decision. With the maintainer's feature freeze noted in the README, a quarterly look at recent commits and a trial run of TruffleHog on one repository cost little and avoid a forced migration later. Route Gitleaks and TruffleHog findings into your vulnerability management plan so each one has an owner and a due date.
Rehearse the rewrite once on a throwaway repository. GitHub warns that the rewrite becomes irreversible once you move past the check of affected pull requests [5], so the first attempt should not happen in the middle of an incident.
Evidence note: UnpanicTech did not run Gitleaks, TruffleHog or git-filter-repo for this article. Commands and flag behavior come from the cited project documentation, which was opened and read on November 13, 2026. Recommendations marked as our analysis are judgment, not benchmark results.
Sources & References
- Gitleaks project, "gitleaks/gitleaks" repository README: https://github.com/gitleaks/gitleaks
- Gitleaks project, ".pre-commit-hooks.yaml" in gitleaks/gitleaks: https://github.com/gitleaks/gitleaks/blob/master/.pre-commit-hooks.yaml
- Truffle Security, "trufflesecurity/trufflehog" repository README: https://github.com/trufflesecurity/trufflehog
- Truffle Security, "TruffleHog Pre-Commit Hooks" (PreCommit.md): https://github.com/trufflesecurity/trufflehog/blob/main/PreCommit.md
- GitHub Docs, "Removing sensitive data from a repository": https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository
Automated pre-commit hooks are critical when developers use AI assistants—as demonstrated by recent secrets leaked on GitHub via local agent config files and CI/CD GhostAction GitHub Actions credential theft campaigns.
- Cloud AI Quota & Gateway Theft: Learn how attackers weaponize leaked AWS Bedrock and Anthropic API keys in our LLMjacking detection and response playbook and the LiteLLM SQL injection CVE-2026-42208 advisory.
- Credential Storage & SSH Hardening: Review architectural standards for how credentials should be stored, lock down developer jump servers with Linux bastion host hardening, and browse our full security hardening guide.

Leave a Comment (Authenticated Users)