Security Fundamentals

Secure Git Repositories and Prevent Secret Leaks

Reduce the risk of credential exposure through repository permissions, secret scanning, and incident response.

Git history is replicated across developer machines, CI systems, mirrors, and backups. A secret committed once may remain accessible even after the visible file is removed.

Prevent secrets from entering history

Use secret managers and environment-specific injection rather than committing credentials. Add appropriate ignore rules, but do not treat .gitignore as a security boundary. It does not remove files that are already tracked.

Enable secret scanning and pre-commit checks where practical. These controls are useful safeguards, not guarantees.

Respond to an exposed credential

Revoke or rotate the credential first. Removing it from current files or rewriting history does not make the old credential safe. Then assess access logs and downstream systems for misuse.

History rewriting may reduce accidental exposure in some contexts, but it requires coordination and cannot erase copies held by others.

Limit repository access

Use least-privilege team permissions, multi-factor authentication, protected branches, and reviewed automation permissions. Audit deploy keys and tokens periodically.

Checklist

  • Keep secrets in managed configuration systems.
  • Enable secret scanning and review alerts.
  • Rotate exposed credentials immediately.
  • Coordinate history cleanup when appropriate.
  • Audit repository and automation access.
Share this page Help other developers discover this resource.