SecretsGitHub
Secret scanning: Gitleaks and GitHub push protection
Push protection blocks a push that contains a secret before it reaches the repository. Gitleaks scans earlier, in a pre-commit hook, and again in CI. GitHub secret scanning covers what is already stored.

Gitleaks as a pre-commit hook
Gitleaks has three scanning commands: git scans Git repositories, dir scans directories or files, and stdin reads from standard input. Version 8.19.0 deprecated detect and protect. Those two are still available but hidden in the --help menu.
Create .pre-commit-config.yaml at the root of the repository:
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.30.1
hooks:
- id: gitleakspre-commit autoupdate
pre-commit installpre-commit autoupdate updates the configuration to the latest version of each repository, and pre-commit install installs the hook. v8.30.1 is the newest Gitleaks release on GitHub today.
The README notes that prepending SKIP=gitleaks to the commit command skips the hook, so scan again in CI.
The same README carries a notice from the author: Gitleaks is feature complete, no new features will be merged, and future releases will contain security patches only. It adds that the author is shifting focus to a project named Betterleaks.
Gitleaks in CI
This workflow comes from the README of gitleaks/gitleaks-action, without its manual and scheduled triggers and with actions/checkout at v7:
name: gitleaks
on:
pull_request:
push:
jobs:
scan:
name: gitleaks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
- uses: gitleaks/gitleaks-action@v3
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITLEAKS_LICENSE: ${{ secrets.GITLEAKS_LICENSE }}fetch-depth: 0fetches all history for all branches and tags.GITLEAKS_LICENSEis required for repositories that belong to an organization account. The README describes the license key as free. Repositories that belong to a personal account need no key.- Since v2.0.0 the action is no longer under the MIT license. It has its own license file.
Outside the action, run the binary or the ghcr.io/gitleaks/gitleaks image:
gitleaks git -v
gitleaks dir -v path_to_directory_or_fileThe exit code when leaks have been encountered is 1 by default. The --redact flag redacts secrets from logs and standard output.
GitHub secret scanning
Secret scanning scans the entire Git history on all branches of a repository for hardcoded credentials. GitHub also scans issues, pull requests, GitHub Discussions, wikis and secret gists.
- Public repositories. Secret scanning runs automatically for free.
- Organization-owned private and internal repositories. These get it when GitHub Secret Protection is enabled, on GitHub Team or on GitHub Enterprise Cloud.
GitHub Secret Protection is a paid product, and buying it requires a GitHub Team plan or a GitHub Enterprise plan.
Push protection
Push protection stops a push that contains a secret before the secret reaches the repository. The push protection page describes two forms:
- Push protection for users is enabled by default. It stops you from pushing secrets to public repositories on GitHub.
- Push protection for repositories is disabled by default. Four roles can turn it on: repository administrator, organization owner, security manager and enterprise owner. The same page says it requires GitHub Secret Protection to be enabled.
Two GitHub pages differ on that last point. The feature table on "About GitHub Advanced Security" lists push protection as available for a public repository without GitHub Secret Protection.
By default, write access to the repository is enough to bypass a block, and the person gives one of these reasons:
- "It's used in tests" creates a closed alert, resolved as "used in tests".
- "It's a false positive" creates a closed alert, resolved as "false positive".
- "I'll fix it later" creates an open alert.
What to do with a finding
- The push was blocked and the secret is in the latest commit. Remove the secret from the code, run
git commit --amend --all, thengit push. - The secret is in an earlier commit. Find the earliest commit that contains it with
git log, startgit rebase -i <COMMIT-ID>~1, and mark that commit to edit. Remove the secret, then rungit add .,git commit --amend,git rebase --continueandgit push. - The secret reached the repository. GitHub's rule: treat a secret as compromised from the moment it is committed to a repository. It is not sufficient to remove the secret from the codebase. Revoke it, update the services that use it, and check the security logs for unauthorized activity.
- The alert stays open. Secret scanning does not automatically close an alert when the token is removed from the repository. Close it with "Close as", and mark it "Revoked" once that is true.
Baselines and allow lists
Gitleaks ignores any old findings that are present in a baseline, and a baseline can be any Gitleaks report:
gitleaks git --report-path gitleaks-report.json
gitleaks git --baseline-path gitleaks-report.json --report-path findings.jsonThe second report contains only new issues. GitHub's rule still applies to the secrets in the baseline: they were committed, so treat them as compromised and rotate them.
Gitleaks has three ways to ignore a finding:
- A
gitleaks:allowcomment on the line tells Gitleaks to ignore that secret. - A fingerprint added to a
.gitleaksignorefile at the root of the repository ignores that specific finding. The README marks this feature as experimental. - An allow list in the configuration file skips commits, paths or patterns:
[extend]
useDefault = true
[[allowlists]]
description = "global allow list"
paths = ['''gitleaks\.toml''']The path in this example comes from the README, so put your own there. [[allowlists]] replaced [allowlist] in v8.25.0.
Where to start
- Check whether push protection is enabled on your repositories and on your own account.
- Add the Gitleaks job to CI with a baseline, so that only new findings are reported.
- Add the pre-commit hook, and ask each collaborator to set it up.
Sources
Want a second pair of eyes on yours? We do this work for a living.
More guides

A secret leaked to Git: what to do in the first hour
Secrets · 4 min read
Revoke first, read the logs, then clean the history, and know what a force push leaves behind.

Secrets in CI/CD: where they leak and how to stop it
Secrets · 4 min read
Six places a pipeline secret escapes, and the GitHub Actions settings that close each one.

SOPS: encrypted secrets in a Git repository
Secrets · 4 min read
Encrypt values with age or KMS, set rules in .sops.yaml, and rotate keys in the documented order.