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?
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
- Prevent. Keep
.env*,*.pem,*.log,dist/,node_modules/and similar in.gitignore. Commit a.env.examplewith empty values. Tell the agent never to read or print secret files, and to use environment variables. - Scan before commit. Run
git diff --stagedand 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. - If a secret was staged, not committed:
git restore --staged .env.local, add it to.gitignore. - If it was committed locally, not pushed: remove it from the commit with
git commit --amendor a reset, then confirm withgit log -p. Still rotate if it ever left your machine. - 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.
- Untrack a file that should be ignored:
git rm --cached pathand 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.examplewith empty values. - Review
git diff --stagedand use a secret scanner and push protection as layers, knowing neither catches everything. - Use
git rm --cachedto 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
.gitignoreafter 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?
Check yourself · 3 of 3
Which layered set of defences best keeps agent junk and secrets out of commits?
Practice items, not counted towards your mastery.
Start here
- Pro Git book (opens in a new tab)BookFree
The full reference for staging, branching, reset and revert, written by Git's maintainers and community.
- Conventional Commits (opens in a new tab)DocsFree
A short, widely used commit message format you can paste into an agent's rules.
- Push protection (GitHub Docs) (opens in a new tab)DocsFree
How GitHub blocks pushes that contain secrets, and what to do when a push is blocked.
More for this topic
- Ignoring files (GitHub Docs) (opens in a new tab)DocsFree
How .gitignore works and how to stop tracking a file that was already committed.