Skip to content
mentoor

Topic 5 of 5 · 5 min read

Keeping secrets and junk out of commits

Goal: Find a leaked secret or generated file in an agent's staged changes and pick the right fix, including key rotation.

Treat any secret that reached a commit as leaked, rotate it first, and then clean the history; and stop junk files at the door with ignore rules and scanning. Deleting the line in a later commit does not remove it from history.

Check yourself · 1 of 3

A secret key was pushed in a commit. What comes first?

Choose one answer

Why it matters

An agent that runs your app reads .env files, writes logs, builds output and sometimes copies a key into a test or a config "to make it work". With git add . all of that can be staged. Once a secret is pushed, bots and anyone with repository access can read it from history, even if the file was deleted in the next commit. GitHub's push protection blocks many known secret formats at push time, but it cannot recognise every kind of secret, so it is a net and not a plan.

How to do it

  1. Prevent. Keep .env*, *.pem, *.log, dist/, node_modules/ and similar in .gitignore. Commit a .env.example with empty values. Tell the agent never to read or print secret files, and to use environment variables.
  2. Scan before commit. Run git diff --staged and look for key-like strings and files you did not expect. Add a pre-commit secret scanner and enable push protection or secret scanning on the host.
  3. If a secret was staged, not committed: git restore --staged .env.local, add it to .gitignore.
  4. If it was committed locally, not pushed: remove it from the commit with git commit --amend or a reset, then confirm with git log -p. Still rotate if it ever left your machine.
  5. If it was pushed: rotate first. Revoke the key in the provider, issue a new one, and update the deployment. Only then remove it from history if you need to, knowing clones and forks may already hold it.
  6. Untrack a file that should be ignored: git rm --cached path and commit; ignoring does not affect files already tracked.
+ STRIPE_SECRET_KEY=sk_live_...redacted
+ DB_URL=postgres://admin:...redacted@db.internal/app

Both lines in a committed .env mean two credentials to rotate.

Before and after

Before: an agent commits .env and debug.log with a "fix config" message. After: both are in .gitignore, the keys are rotated, and .env.example documents the variable names.

What goes wrong

  • Delete and forget. The key stays in history and still works.
  • Rewriting history without rotating, which leaves live keys in forks and caches.
  • Junk commits of dist/ or logs that bloat the repository and create conflicts.

Examples

Pushed key, wrong fix

An agent committed a cloud access key in config.ts, and the developer pushed it, then deleted the line in a follow-up commit. The key is still in history and still valid. The right order: revoke and replace the key at the provider, deploy the new one, then remove it from history if policy requires.

Ignored too late

$ echo ".env" >> .gitignore
$ git status --short
 M .env

.env is still tracked, so ignoring it changes nothing. Run git rm --cached .env, commit, and rotate any secrets it held because they are in earlier commits.

Key points

  • A secret that reached a commit is leaked; rotate it first, then clean up the history.
  • Deleting the secret in a later commit does not remove it from history.
  • Keep env files, keys, logs and build output in .gitignore, and commit only a .env.example with empty values.
  • Review git diff --staged and use a secret scanner and push protection as layers, knowing neither catches everything.
  • Use git rm --cached to stop tracking a file that was committed before it was ignored.

Common mistakes

  • Deleting the secret in a new commit and considering it fixed.
  • Rewriting history without rotating the key.
  • Adding a file to .gitignore after it is already tracked and expecting it to disappear.
  • Trusting push protection to catch every kind of secret.

Check yourself · 2 of 3

Why does adding .env to .gitignore not stop a file that is already tracked?

Choose one answer

Check yourself · 3 of 3

Which layered set of defences best keeps agent junk and secrets out of commits?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic