This guide will help you set up Gitleaks to prevent committing secrets to the repository.
Windows (Chocolatey - Recommended):
choco install gitleaksWindows (Scoop):
scoop install gitleaksWindows (Manual):
- Download the latest release from https://github.com/gitleaks/gitleaks/releases
- Extract
gitleaks.exeto a folder in your PATH (e.g.,C:\Program Files\gitleaks\) - Verify installation:
gitleaks version
macOS:
brew install gitleaksLinux:
# Download and install
wget https://github.com/gitleaks/gitleaks/releases/download/v8.21.2/gitleaks_8.21.2_linux_x64.tar.gz
tar -xzf gitleaks_8.21.2_linux_x64.tar.gz
sudo mv gitleaks /usr/local/bin/This will automatically scan for secrets before each commit.
ip install pre-commit
pre-commit installWindows:
.\scripts\scan-secrets.ps1Linux/Mac:
chmod +x scripts/scan-secrets.sh
./scripts/scan-secrets.shOnce pre-commit is installed, Gitleaks will run automatically before each commit:
git add .
git commit -m "Your commit message"
# Gitleaks runs automatically hereIf secrets are detected, the commit will be blocked and you'll see a report.
Scan entire repository:
# Windows
.\scripts\scan-secrets.ps1
# Linux/Mac
./scripts/scan-secrets.shScan with verbose output:
# Windows
.\scripts\scan-secrets.ps1 -Verbose
# Linux/Mac
./scripts/scan-secrets.sh --verboseScan all files (including uncommitted):
# Windows
.\scripts\scan-secrets.ps1 -AllFiles
# Linux/Mac
./scripts/scan-secrets.sh --all-files# Scan the entire repository
gitleaks detect --source . --config .gitleaks.toml
# Scan with verbose output
gitleaks detect --source . --config .gitleaks.toml --verbose
# Scan specific commits
gitleaks detect --source . --log-opts "origin/main..HEAD"
# Scan without git (all files)
gitleaks detect --source . --no-git- Review the findings in
gitleaks-report.json - Remove the secret from your code
- Use environment variables instead:
import os api_key = os.environ.get('JINA_API_KEY')
- Copy
.env.exampleto.envand add your secrets there (.envis gitignored) - If the secret was already committed, you need to:
- Remove it from git history (use
git filter-branchor BFG Repo-Cleaner) - Rotate/regenerate the credential
- Never push the commit with the secret
- Remove it from git history (use
If Gitleaks flags something that isn't a secret:
- Verify it's truly not sensitive
- Edit
.gitleaks.tomlto add an allowlist entry:
[allowlist]
regexes = [
'''your-false-positive-pattern'''
]
# Or allowlist specific files
paths = [
'''tests/test_example\.py'''
]The repository includes GitHub Actions workflow (.github/workflows/gitleaks.yml) that automatically scans:
- All pushes to main/master/develop branches
- All pull requests
- Manual workflow runs
Gitleaks configuration is in .gitleaks.toml. It includes:
- Default rules: All standard Gitleaks secret detection patterns
- Custom rules:
- Google Service Account JSON detection
- Jina API key detection
- GCP Project ID detection (for review)
- Allowlists: Documentation files, examples, and known false positives
- ✅ Use
.envfiles for local secrets (already in.gitignore) - ✅ Use environment variables in code
- ✅ Run scans before pushing to remote
- ✅ Keep service account files outside the repository
- ✅ Use placeholders in documentation (e.g.,
your-api-key) - ❌ Never hardcode credentials
- ❌ Never commit
.envfiles - ❌ Never commit service account JSON files
"gitleaks: command not found"
- Gitleaks is not installed or not in PATH
- Follow installation instructions above
Pre-commit hook not running
- Run
pre-commit installto set up the hook - Verify with
pre-commit run --all-files
Too many false positives
- Review and update
.gitleaks.tomlallowlist - Use more specific patterns
- Consider using
--verboseto see detection details
- Gitleaks Documentation
- Pre-commit Documentation
- SECURITY.md - Full security policy