← Blog
// BLUE TEAM · DEVSECOPS

A secret in git isn't deleted, it's rotated (and why)

Published Aug 12, 2026 · 7 min read
Blue TeamDevSecOpsGitSecretos

The scene is universal: you accidentally push a .env with an API key, notice five minutes later, run git rm, a "remove secret" commit, and breathe out. Wrong move: the secret is still there, intact, in the history. And by now, someone else probably has it too.

Why deleting it doesn't delete it

Git doesn't store "the current state", it stores every change. Your deletion commit adds a new line to the history, it doesn't rewrite it. The secret is still reachable with a trivial command:

git log -p | grep -i "api_key"
git show <commit-where-you-added-it>

And it's not just your clone. If you pushed it to a shared repo, assume you no longer control it: there are forks, colleagues' clones, provider-cache copies, CI logs and backups. Worse: automated bots scan public commits in real time; a leaked AWS key can be mining crypto in your account within minutes.

First thing, always: rotate

The instinct is "I'll delete the commit and it's fixed". No. The very first thing is to revoke and rotate the credential at the provider (AWS, GitHub, Stripe, your database…). From the second a secret touches a repo, treat it as compromised —even if the repo is private—. Rotating is what cuts the risk; everything else is cleanup.

⚠️ "It's a private repo" won't save you: internal forks, CI logs, integration tokens and backups multiply the copies. A committed secret is a burned secret.

Scrubbing history: useful, but not the cure

Once the credential is rotated, you can rewrite history to remove the trace with git filter-repo (or BFG). But understand its limits:

Where they hide (beyond history)

🔑 Before you commit, run your diff, your .env or your config through the Secret Scanner: it detects AWS keys, GitHub/Slack tokens, private keys, JWTs and high-entropy strings, in your browser (nothing is uploaded). The cheapest way to catch the secret is to never let it reach the repo. And if the leak is a JWT, see also why a token reads without a key.

Prevention that actually works

Checklist for a leak

"I already deleted it" is the sentence that precedes the AWS bill. In git, a secret isn't deleted: it's rotated. Internalize that difference and you'll dodge the incident that starts with "but I removed it in the next commit".

Share: LinkedIn X
Sergio Belmonte Morales
Sergio Belmonte Morales
Cybersecurity Analyst · SOC · Sentinel/KQL specialist