الانتقال إلى المحتوى
الأمان (Security)إدارة الـ Secrets· 7 دقائق للقراءة

Deleting a Leaked Secret Isn't Enough: Git History, Env Files and Rotation

A new commit or a .env file hides the key from today's code. The old commits, every clone and the key itself keep working until you revoke it.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

A developer on a small team opens a pull request on the company's public storefront plugin. A reviewer spots the payment provider's API key in appsettings.json. The developer empties the value, moves the key to a .env file listed in .gitignore, and pushes a commit called "Move API key to .env". The review passes, the branch is merged, and nobody touches the key itself. A month later the provider's dashboard shows refunds that nobody on the team issued. The key was still in the first commit, and it still worked.

The common misconception

The belief: once a secret is gone from the code, by a new commit or by moving it to an env file, it's safe again.

Every tool you look at agrees. The file on GitHub shows an empty value. git grep finds nothing. Code search finds nothing. Moving secrets out of source files is also genuinely good advice, so the commit looks like the responsible fix. But all of those tools show the latest version of the code, and a leaked secret doesn't care about the latest version. It cares about every copy that already exists, and about whether the key is still valid.

What actually happens

A commit keeps the old version forever

A is a snapshot of the whole project. Deleting a line adds a new snapshot; it doesn't edit the old ones. We reproduced the story in a scratch repository on Git 2.54, with an obviously fake key, and searched for it after the "fix":

Bash terminal
git grep FAKE-KEY                  # the current files: no outputgit log --oneline -S FAKE-KEY      # commits that added or removed it# a446c90 Move API key to .env# 97d35a1 Add payments configgit show 97d35a1:appsettings.json# {#   "Payments": { "ApiKey": "FAKE-KEY-0000-DO-NOT-USE" }# }git show a446c90 -- appsettings.json | grep FAKE# -  "Payments": { "ApiKey": "FAKE-KEY-0000-DO-NOT-USE" }

Notice the last line. The cleanup commit itself shows the key, as the line it removed. Anyone who reads the pull request diff or the commit page sees it, and a fresh clone of the repository returned the same two commits.

Copies don't receive your cleanup

Even a full history rewrite only changes the repository you push it to. GitHub's guide on removing sensitive data lists where the commits "may still be accessible elsewhere": "In any clones or forks of your repository", "Directly via their SHA-1 hashes in cached views on GitHub" and "Through any pull requests that reference them". It's blunt about the first: "You cannot remove sensitive data from other users' clones of your repository." CI caches, build logs and laptops are copies too.

A rewrite can even help an attacker. GitHub warns that "clueful users with an existing clone will notice the history divergence and can use it to quickly and easily find the sensitive data still in their clone that you removed from the central repository."

An env file moves the key, it doesn't change it

The value in .env is the same string that sits in history, so the move changes where your next deploy reads it, not whether the leaked copy works. Env files and variables have their own exposure, too: OWASP's Secrets Management Cheat Sheet notes that environment variables "are generally accessible to all processes and may be included in logs or system dumps." And if the key ships inside a mobile app, it's public whatever you do in Git, as the article "Your Mobile App Can't Keep a Secret" explains.

Only revocation makes the leaked copy useless

An API key is a : whoever presents it gets in. So the fix happens at the provider, not in the repository. GitHub's guide starts there: "as a first step you need to revoke and/or rotate that secret. Once the secret is revoked or rotated, it can no longer be used for access, and that may be sufficient to solve your problem." Its secret scanning docs add that removing secrets from history "is time-intensive and often unnecessary if you've already revoked the credential." OWASP's incident response order is revocation, , deletion from the exposed system, then logging: "Who had access? When did they use it?"

Closing the leak in the right order

Bash terminal
# Empty the value, move the key to .env, push. Incident closed?git commit -am "Move API key to .env"git push# The key is still in the previous commit, in this commit's diff,# in every clone and fork, and it still works at the provider.

Step 1 is the only one that stops the leaked key from working, and it's the one the wrong tab skips. Steps 2 and 3 cost minutes. Step 4 uses the commands from GitHub's guide; --sensitive-data-removal needs git-filter-repo 2.47 or later. Git's own documentation calls the older git filter-branch a tool whose "use is not recommended" and points to git filter-repo instead.

ActionGone from current filesGone from historyLeaked key stops working
A new commit that deletes the keyYesNoNo
Moving the key to a .env fileYesNoNo
Rewriting history and force-pushingYesOn the server only; clones and forks keep itNo
Revoking and rotating the keyNo, by itselfNoYes

How to apply it in your project

  1. Write the runbook before you need itFor each secret, note who can revoke it, how to issue a new one and where the app reads it. OWASP's cheat sheet asks for exactly this, including "Who is the point of contact during an incident".
  2. Revoke and rotate firstWhen a secret leaks, revoke it at the provider, issue a new one, put it in your secret store and redeploy. Moving the old value somewhere else doesn't count.
  3. Check how the old key was usedRead the provider's access logs for the exposure window. Look for calls from unknown addresses or actions your app never makes.
  4. Decide whether history still needs cleaningRotation usually ends the risk. Rewrite with git-filter-repo only when the data can't be rotated, such as personal data, and plan for clones, forks and open pull requests.
  5. Stop the next leak at the sourceIgnore env files, turn on push protection and secret scanning, and add a pre-commit secret scan, which OWASP suggests so secrets are caught "before commit/PR".

Key takeaways

  • Deleting a secret in a new commit leaves it in every earlier commit, and the cleanup diff shows it too.
  • Clones, forks, caches and pull requests keep their copies; a history rewrite can't reach them.
  • Moving a key to a .env file changes where it's stored, not whether the leaked copy works.
  • Revoke and rotate first: it's the only step that makes the leaked key useless.
  • Treat history rewriting as optional cleanup, and use push protection to stop the next leak.

📚 المصادر

  1. Secrets Management Cheat Sheet — OWASP Cheat Sheet Series
  2. Removing sensitive data from a repository — GitHub Docs
  3. Secret scanning — GitHub Docs
  4. Push protection — GitHub Docs
  5. git-filter-branch — Git reference

التعليقات (0)