Gitleaks — Find Secrets in Git Repos and Code
Gitleaks is a fast SAST tool for detecting hardcoded secrets like passwords, API keys, and tokens in Git repositories. It scans commit history and source code using regex patterns, preventing secret leaks before they reach production.
What it is
Gitleaks is a fast static application security testing (SAST) tool for detecting hardcoded secrets in Git repositories. It scans commit history and source code using regex patterns to find AWS keys, database passwords, API tokens, private keys, and other credentials that should never be committed.
Gitleaks is designed for security engineers and developers who want to prevent secret leaks before they reach production, integrated into CI/CD pipelines and pre-commit hooks.
How it saves time or tokens
Gitleaks scans entire Git histories in seconds, catching secrets that were committed and later deleted but still exist in history. Running it as a pre-commit hook prevents secrets from being committed in the first place. This avoids the costly process of rotating compromised credentials after a leak.
How to use
- Install Gitleaks:
brew install gitleaks
# Or: go install github.com/gitleaks/gitleaks/v8@latest
- Scan your repository:
# Scan all commits
gitleaks detect
# Scan only staged changes (pre-commit)
gitleaks protect --staged
# Scan a directory without Git
gitleaks dir -s ./src
- Add as a pre-commit hook for continuous protection
Example
# Output results as JSON for CI integration
gitleaks detect --report-format json --report-path results.json
# Custom config for additional patterns
cat > .gitleaks.toml << 'EOF'
[[rules]]
id = 'custom-api-key'
description = 'Custom API Key'
regex = '''MYAPP_API_KEY=[a-zA-Z0-9]{32}'''
[rules.allowlist]
paths = ['test/', 'docs/']
EOF
gitleaks detect -c .gitleaks.toml
Related on TokRepo
- Security tools — security scanning and auditing resources
- DevOps tools — CI/CD pipeline integration tools
Common pitfalls
- Not scanning Git history (use
detectnot justdir), which misses secrets in deleted commits - Generating too many false positives without configuring allowlists for test data
- Running Gitleaks only in CI without a pre-commit hook, allowing secrets to enter the repo first
Frequently Asked Questions
Gitleaks scans the entire Git history by default and comes with a comprehensive built-in ruleset for common secret patterns. git-secrets by AWS focuses on preventing commits and requires manual rule configuration. Gitleaks is faster and has broader pattern coverage.
Yes. Gitleaks provides a GitHub Action, and works in any CI system. Run gitleaks detect in your pipeline and fail the build on findings. JSON output integrates with security dashboards.
Add a .gitleaks.toml config with allowlists for specific paths, commits, or patterns. You can also add inline comments with gitleaks:allow to suppress specific findings.
Yes. Use gitleaks dir -s ./path to scan any directory without Git history. This is useful for scanning build artifacts, config files, or code before it enters version control.
Gitleaks detects AWS keys, GCP credentials, Azure tokens, GitHub tokens, private keys, database connection strings, API keys for major services, JWTs, and many more through its built-in regex rules.
Citations (3)
- Gitleaks GitHub— Gitleaks SAST tool for detecting secrets
- Gitleaks Documentation— Secret detection regex patterns
- OWASP SAST— Static application security testing best practices
Related on TokRepo
Discussion
Related Assets
HumHub — Open-Source Enterprise Social Network
A flexible, open-source social networking platform built on Yii2 for creating private communities, intranets, and collaboration spaces within organizations.
Dolibarr — Open-Source ERP & CRM for Business Management
A modular open-source ERP and CRM application written in PHP for managing contacts, invoices, orders, inventory, accounting, and more from a single web interface.
PrestaShop — Open-Source PHP E-Commerce Platform
A widely adopted open-source e-commerce platform written in PHP with a rich module marketplace, multi-language support, and a strong European user base.