Skip to content
ShipSafeStack

Checklist

Committed a .env file to GitHub? What to do right now

Short answer. Treat every value in the file as copied. Revoke or rotate each real secret at its provider, put the new values in the host's environment, and redeploy. Deleting the file or the commit does not retire a key that already left the repo. Rewriting history comes after the rotation.

Updated 2 Dec 2026

This is a practical checklist, not a guarantee. It is not a penetration test, a compliance audit, or proof that an app is safe to launch.

A .env file is a list of credentials. GitHub's leaked-secret tutorial says to consider any leaked secret immediately compromised, and that removing it from the codebase, pushing a new commit, or deleting and recreating the repository does not prevent exploitation. The same page says automated scanners can locate publicly exposed secrets in minutes. This page is the first sitting after the push. It is a checklist, not a statement that the account is fine.

Rotate before you touch git

Do this before you delete the file, revert the commit, or rewrite history. GitHub's removal page says that if the data is a password, token, or credential, revoke or rotate it first. Once it can no longer be used, that page says you generally do not need to rewrite history.

Open the file and list every name. Copy the names onto a note you control. Do not paste the values into a chat, an issue, or a support ticket. Then revoke or rotate each real secret at the provider that issued it, and only then put a new value on the host.

Use the matching page for the value you actually have:

A database URL that contains a password is a secret even when the hostname looks ordinary. A private key, a whsec_ webhook secret, and an email API key are secrets. If you cannot tell what a line is, rotate it. Leaving it is the larger mistake.

Values that are public by design

Some lines in a .env are identifiers, not passwords. Rotating them does not fix access rules, and treating them as the only problem can hide the real secret on the next line.

A public identifier in a public repository is still a reason to check the rules behind it. It is not, by itself, the same job as revoking a secret key.

Read the provider's usage

After the revoke, look for use you do not recognize. GitHub's tutorial says to review the provider's logs. For AWS it points at CloudTrail. The other providers document their own pages, and those steps are on the leak guides above: OpenAI's Usage page, Stripe's events, and the GitHub security log for a token.

A quiet page does not mean the value was never copied. GitHub says scanners can find a public secret in minutes, and that exploitation can follow within hours. Check the log. Rotate anyway.

Stop tracking the file

This commit does not erase history. It stops the next push from adding the file again.

1. Add .env to .gitignore. GitHub's removal page says to commit and push that ignore rule so other people are covered too. The Node gitignore maintained in github/gitignore ignores .env and .env.*, and keeps .env.example with the line !.env.example. If you also committed .env.local or .env.production, ignore those names too. 2. Keep a local copy of the names until the new values are on the host. Then untrack the file without deleting your working copy: git rm --cached .env. Repeat for each extra path you committed. 3. Commit the ignore rule and the untrack. Do not put the old secret in the commit message. GitHub's removal page says to avoid git add . and git commit -a on the next commits, and to review git diff --cached before you commit. 4. Add a .env.example that lists the names and empty values, with no real keys. That is the file the Node gitignore leaves tracked.

The new values belong on the host, not in the repository. Vercel's environment variables page says you can import a .env in the project settings, that the stored values are encrypted at rest, and that a change applies only to new deployments. Redeploy after you save. A name the framework inlines for the browser, such as NEXT_PUBLIC_ or VITE_, is still public. The wider pass is API keys in the frontend.

Rewrite history only after the revoke

GitHub's secret scanning page says removing a secret from git history is time-intensive and often unnecessary once the credential is revoked. The removal page says it only recommends a rewrite for data that cannot be revoked or rotated, such as personal data. Do the rewrite when you still need the file gone from history, or when a value in the file cannot be rotated. Do it after the revoke, not instead of it.

The current removal page uses git-filter-repo, version 2.47 or newer, because that is the version with --sensitive-data-removal. It does not document the BFG Repo-Cleaner. Follow the page you open today if a step here drifts.

On a fresh clone, GitHub's command to drop one file from every branch, tag, and ref is:

git-filter-repo --sensitive-data-removal --invert-paths --path .env

Use the path inside the repository, not only the filename. If the file used to live somewhere else, add another --path or run the command again for that path. To replace the secret strings themselves, GitHub shows git-filter-repo --sensitive-data-removal --replace-text ../passwords.txt, with the strings in a file that is not inside the repository.

Before you push, GitHub says to count the pull requests the rewrite will affect, from .git/filter-repo/changed-refs. After the next push, the rewrite is irreversible. Then:

git push --force --mirror origin

GitHub includes --force as a reminder that this updates every branch, tag, and ref and discards other people's updates to those refs while you were cleaning. Pushes of refs/pull/ fail because GitHub marks them read-only. Other failures usually mean branch protection is blocking the force push. Turn that protection off temporarily, push again, and turn it back on. GitHub says to merge or close open pull requests before you remove files.

After the force push, commits are still reachable by SHA in cached views, in forks, and in pull requests that reference them. You cannot delete someone else's clone. GitHub points collaborators at the git-filter-repo manual: they rebase onto the new history, they do not merge, because one merge can bring the old history back. A fork keeps the commit until that fork's owner removes it or deletes the fork. GitHub says it cannot give you those owners' contact details.

Contact GitHub Support when the data cannot be made useless by rotation. GitHub says Support will not remove non-sensitive data, and will only help remove sensitive data when they decide rotation does not address the risk. The ticket needs the owner and repository name, the count of affected pull requests, the first changed commits from the git-filter-repo output, and the kind of data. Do not include the secret values or the file contents. Support then tells you the remaining steps, which is how cached views and pull-request refs come off GitHub.

Secret scanning and push protection

Secret scanning runs automatically, for free, on public repositories. It scans all branches and git history, and GitHub also scans issues, pull requests, discussions, wikis, and secret gists. Organization-owned private and internal repositories need GitHub Secret Protection on GitHub Team or GitHub Enterprise Cloud. User-owned repositories are covered on GitHub Enterprise Cloud with Enterprise Managed Users, and on GitHub Enterprise Server when the enterprise has GitHub Secret Protection. A typical user-owned private repository is not in that list. GitHub Advanced Security is the directory entry. The plan still decides what a private repository gets.

Push protection for users is on by default on GitHub.com and blocks pushes of detected secrets to public repositories. It does not cover private repositories. Push protection on a specific repository requires GitHub Secret Protection, is off until someone turns it on, and by default anyone with write access can bypass it. A bypass is not a reason to leave the file in the commit. Push protection only sees patterns GitHub supports. A custom password in .env can still go through.

If a partner pattern is found in a public repository, GitHub says it reports the leak to the provider, and the provider may revoke it. That report is not a substitute for you revoking the key and checking usage.

If an AI builder synced the repo

Lovable, Bolt, Cursor, and Replit do not change what a committed .env is. A GitHub export or sync commits the files in the project, including a .env the agent wrote. The longer note is How to keep API keys out of AI-generated code. Browser-inlined names are API keys in the frontend.

Ignore .env before the first sync, and keep real keys in the host or in the builder's server-side secret store. After a sync that already pushed the file, rotate first, then clean the GitHub repository with the steps above. Deleting the file in the builder does not revoke the key, and it does not erase the commit GitHub already has.

Checklist

  • List every name in the file. Rotate each real secret at its provider before you delete anything.
  • Read that provider's usage or activity log for calls you do not recognize.
  • Put the new values in the host's environment and redeploy. Vercel applies environment changes on the next deployment.
  • Add .env to .gitignore, run git rm --cached on each committed path, commit a .env.example with empty values, and push that commit.
  • If the value cannot be rotated, or you still need it out of history, rewrite with git-filter-repo as GitHub documents, force-push with --mirror, and have every clone rebase. Ask GitHub Support about cached views only when rotation does not address the risk.
  • Turn on push protection where your plan includes it. Confirm push protection for users is still on.
  • Then run a free passive check on the public URL. It can see a key that shipped in a public script. It does not read a private repository or a provider account. The rest of the pass is the pre-launch checklist. The worksheets are the checklist pack.

FAQ

I deleted .env and pushed. Is the leak over?

No. GitHub's tutorial says removing the secret from the codebase, or deleting the repository, does not prevent exploitation. The old commit, any fork, and any clone still have the values until you revoke them at the provider. git rm --cached only stops the file from being tracked in later commits.

Do I have to rewrite history?

Not always. GitHub's removal page says that once a secret is revoked or rotated, you generally do not need to rewrite history, and that a rewrite is what they recommend for data you cannot rotate. If you do rewrite, use the current git-filter-repo steps, expect new commit hashes, and expect forks and cached SHAs to keep the old commit until those copies are cleaned.

Will secret scanning warn me about a private hobby repository?

Not by the rule GitHub publishes. Secret scanning is automatic on public repositories. Organization-owned private repositories need GitHub Secret Protection. User-owned private repositories are not in the availability list, except the enterprise cases GitHub names. Push protection for your user blocks detected secrets on pushes to public repositories only.

The file only had a Supabase anon key and a Sentry DSN. Do I rotate those?

Read the two pages first. Is the Supabase anon key safe to expose? and Is the Sentry DSN safe to expose? describe values those vendors document for the client. Still read the rest of the file. A service role key, a Stripe secret, or an auth token on the next line is a different job, and that one is a revoke.

Sources

Checked 2 Dec 2026.

Informational only. This page describes checks and vendor documentation. It is not a security audit, a penetration test, or a certification, and it is not a guarantee.

Newsletter

A short weekly note on new tools and security checklists. No spam, unsubscribe anytime.