$ run as a normal user# run as root (or with sudo)
Keep secrets out of Git
A secret in Git history is public forever for everyone who can clone — even after you delete the file.
Scan for secrets
Scanners find keys and passwords by patterns and entropy — locally, in pre-commit hooks and in CI.
Stop mistakes with pre-commit hooks
A hook is a script Git runs at an event. A pre-commit hook can stop a commit with a secret, a format error or a failing check.
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.24.0
hooks:
- id: gitleaks
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.96.1
hooks:
- id: terraform_fmt
- id: terraform_validatepip install pre-commit
pre-commit install
pre-commit run --all-filesRemember
Local hooks can be skipped (--no-verify). Run the same checks again in CI — the pipeline is the real gate.
Sign your commits
Anybody can set any name and email in a commit. A signature proves the commit came from your key. GitHub shows it as Verified.
git config --global gpg.format ssh
git config --global user.signingkey ~/.ssh/id_ed25519.pub
git config --global commit.gpgsign true
git config --global tag.gpgsign true
git commit -m "Signed change"
git log --show-signature -1Add the same public key in GitHub → Settings → SSH and GPG keys → Signing key.
React to a leaked secret
Assume a leaked secret is already used by an attacker. Order: revoke, then clean.
- Revoke / rotate the secret at once (new key, new password).
- Check the logs of that system for misuse.
- Remove it from the code and add a check so it does not happen again.
- Optional: rewrite the history (
git filter-repo) and force-push — only after rotation, and tell everyone to re-clone.
pip install git-filter-repo
git filter-repo --path secrets.env --invert-pathsWatch out
Rewriting history does not remove the secret from forks, clones and caches. Rotation is the only real fix.