Git Emergency

I pushed my .env file to GitHub

Severity Critical

What just happened

Your secrets (API keys, passwords, tokens) are in the repository history. Bots scan public repositories within seconds. Removing the file in a new commit does NOT remove it from history.

Potential risks

  • Leaked keys can be abused immediately (cloud bills, data theft).
  • Forks and clones keep a copy even after you clean history.

Before you run anything

  • STEP ZERO: revoke and regenerate every secret in the file. Cleaning history is not enough.
  • Check your providers' logs for suspicious use.
  • History rewriting requires a backup clone and the team's agreement.
  • Golden rule: before any destructive command, create a safety branch with git branch backup/before-fix โ€” it costs nothing.

Recovery steps

  1. 1

    Stop tracking the file

    Removes .env from the index but keeps it on your disk, then prevents it from coming back.

    Safe
    $ git rm --cached .env
    $ echo ".env" >> .gitignore
    $ git commit -m "chore: stop tracking .env"
  2. 2

    Make a backup clone

    A mirror clone, just in case the next step goes wrong.

    Safe
    $ git clone --mirror git@github.com:OWNER/REPO.git backup-repo.git
  3. 3

    Erase the file from all history

    git filter-repo (to install separately) rewrites every commit and all hashes change. It also removes the โ€œoriginโ€ remote: add it back before pushing (git remote add origin <url>).

    Destructive
    $ git filter-repo --force --invert-paths --path .env
  4. 4

    Force push the cleaned history

    Everyone must then re-clone. On GitHub, also contact support to purge cached views and pull request refs.

    Destructive
    $ git push --force --all origin
    $ git push --force --tags origin

These procedures are provided for information only. Read each command and adapt it to your situation before running it. Disclaimer

Other disasters

๐Ÿช“ I deleted my branch๐Ÿšจ I pushed directly to main๐Ÿ’ฅ I ran git reset --hard๐Ÿงจ I overwrote a colleague's commits๐Ÿ”€ I made a bad merge๐Ÿ‘ป I lost uncommitted changes๐ŸŽฏ I committed on the wrong branch๐ŸŽˆ I'm in โ€œdetached HEADโ€ stateโš”๏ธ I'm stuck in a merge conflict๐Ÿšซ My push is rejected (non-fast-forward)๐ŸŒ€ My rebase went wrong๐Ÿ˜ GitHub rejects a file over 100 MB๐Ÿฅธ I committed with the wrong name or email๐Ÿ—‘๏ธ I deleted a file and committed itโช I want to undo my last commitโœ๏ธ I amended a commit that was already pushed๐Ÿงน Too many messy commits to clean up๐Ÿ“ฎ I pushed to the wrong branch๐Ÿงญ I rebased onto the wrong branch๐Ÿ” I need back the changes I reverted๐Ÿ“ฆ git stash pop gave me a conflict๐Ÿ™ˆ I committed a file that should be ignored๐Ÿ”  Git doesn't see my file rename (upper/lower case)๐Ÿงจ I ran git clean -fd by mistake๐Ÿฉป My repository is corrupted๐Ÿ™ƒ I forgot to pull before pushing๐Ÿ‘ฏ Duplicated lines of code after a mergeโž– I added a file I didn't want to (git add)โœ๏ธ I made a typo in a commit messageโœ‚๏ธ My commit does too many things๐Ÿงบ I lost a stash (drop, clear or pop gone wrong)๐Ÿซฅ I pulled and my code disappeared๐Ÿ’ I cherry-picked the wrong commitโ˜๏ธ I deleted a branch on GitHub / GitLab๐Ÿ‘ฅ I pushed with the wrong GitHub account๐Ÿšš The repository was renamed or moved๐Ÿท๏ธ I tagged the wrong commit๐ŸŒŠ Git says every file is modified, but I changed nothing๐Ÿ“ฆ I committed node_modules (or a build folder)๐Ÿ“‚ I ran git init in the wrong folder๐Ÿงจ My pull request has conflictsโณ My pull request is behind main๐Ÿ“ I have to apply review changes to my PR๐Ÿงณ My PR shows commits that aren't mine๐ŸŽฏ I opened my PR against the wrong branch๐Ÿšจ A merged PR broke main๐Ÿšช I closed my PR (or deleted its branch) by mistake๐Ÿ”ด The checks of my PR are failing๐Ÿ˜ My PR is too big to review๐Ÿด My fork is behind the original repository