What is Gitleaks and how do you use it?

BeginnerHands-onGitleaks
Answer

Short answer

Gitleaks is a free tool that scans Git repositories for secrets — passwords, API keys, tokens and private keys — that developers accidentally commit. You run gitleaks detect on a repo, add it as a pre-commit hook to stop leaks before they happen, and run it in CI to fail the build if a secret is found.

Why it matters

Once a secret is pushed to Git, it stays in the history even if you delete it in the next commit. Bots scan public GitHub for AWS keys within minutes. A single leaked cloud key can lead to stolen data or a huge bill for crypto-mining servers.

How Gitleaks works

It reads every file and commit and checks them against rules — patterns for known secret formats (AWS keys start with AKIA, GitHub tokens with ghp_, private keys with -----BEGIN ... PRIVATE KEY-----), plus a check for random-looking high-entropy strings.

Examples

# Install (macOS / Linux with Homebrew) or use the Docker image
brew install gitleaks

# Scan the whole repo including all history
gitleaks detect --source . -v

# Scan only staged changes (before commit)
gitleaks protect --staged -v

# Run with Docker, write a JSON report
docker run --rm -v "$PWD:/repo" zricethezav/gitleaks:latest detect --source /repo --report-path /repo/gitleaks.json

Example finding:

Finding:     aws_secret_access_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
Secret:      wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
RuleID:      aws-access-token
File:        config/settings.py
Line:        12
Commit:      4f2a9c1
Author:      [email protected]

In GitHub Actions:

- uses: actions/checkout@v4
  with: { fetch-depth: 0 }          # full history so old commits are scanned
- name: Gitleaks
  run: |
    docker run --rm -v "$PWD:/repo" zricethezav/gitleaks:latest detect --source /repo -v

If Gitleaks finds a secret it exits with code 1, and the pipeline fails.

What to do when a secret is found

  1. Rotate it immediately — create a new key and disable the old one. Removing it from Git is not enough; assume it's already stolen.
  2. Remove it from the code and load it from a secret store (Vault, AWS Secrets Manager, CI secrets).
  3. If needed, clean the history with git filter-repo — but rotation comes first.
  4. For a false positive, add it to a .gitleaksignore file with a comment explaining why.

What to say in the interview

"Gitleaks scans Git repos for hard-coded secrets using regex rules and entropy checks. We run it in two places: as a pre-commit hook so developers catch leaks before committing, and as the first stage in CI with full history, failing the build on any finding. If a real secret is found, the first step is to rotate it, not just delete it, because it stays in Git history."

Follow-up questions you may get

  • Alternatives to Gitleaks? TruffleHog, GitHub secret scanning with push protection, detect-secrets.
  • Why fetch-depth: 0 in CI? By default, checkout fetches only the last commit; secrets in older commits would be missed.
All Security Tools questions Roadmap: Gitleaks — secret scanning

More on Gitleaks