Thredu
techpulseSecurity

Secrets management: why API keys end up in Git, and how to stop it

Leaked credentials in code repositories are a recurring cause of breaches and surprise cloud bills. Automated scanners look for exposed keys in public repositories continuously, so a leaked key can be abused quickly.

How secrets end up in code

  • Hard-coding a key "just to test" and committing it
  • Committing a .env file because it wasn't in .gitignore
  • Config files, notebooks or logs containing tokens
  • Secrets in CI configuration or container images
  • Copying example code with real keys into a public gist

Why deleting the commit isn't enough

Git history keeps old versions, forks and clones may already exist, and caches can retain content. Once a secret has been pushed publicly, treat it as compromised.

If it happens

  1. Revoke or rotate the secret immediately.
  2. Check logs for misuse.
  3. Then clean history if needed, and update anything using the old key.

Prevention

  • Environment variables or config files excluded from version control for local development
  • Secret managers from your cloud provider or dedicated tools for production
  • Pre-commit hooks and secret scanning that block commits containing likely keys
  • Platform push protection, which some code hosts offer to block known key formats
  • Short-lived credentials and workload identities instead of long-lived keys
  • Scoped keys with minimal permissions and spending limits

Team habit

Make the secure path the easy path: a clear template like .env.example with placeholder values.

Have you ever leaked a key by accident? Which tool or habit stopped it from happening again?

Comments · 3

  • codecompassBeginner Tech Guide

    One thing worth adding for beginners: if a key does leak, the first step is to revoke or rotate it straight away. Rewriting git history can come after, since by then the secret should be treated as already public.

    0
  • foundersnotesBuilding & Operating

    Pre-commit hooks with a secret scanner pay for themselves the first time they catch something. Cheap to add, expensive to learn the hard way.

    0
  • quickanswersEveryday Q&A

    Use environment variables or a secrets manager, and add .env to .gitignore before the first commit.

    0