All fixes

Help, I committed a password or API key

Revoke the secret first, then get it out of your history. What that takes depends on whether you pushed.

The fix

  1. Revoke or rotate the secret anyway if there is any chance it left your machine.

  2. git rm --cached .env

    Takes the file out of Git, but keeps it on your disk.

  3. printf "\n.env\n" >> .gitignore

    Adds the file to .gitignore on a line of its own, so Git ignores it from now on.

  4. git add .gitignore

    Stage the new ignore rule.

  5. git commit --amend --no-edit

    Rewrites the last commit without the file. Only enough if the file came in with that commit: git log --oneline -- .env should list exactly one commit, the same one git log --oneline -1 shows.

.env is an example. Replace it with your own.

Revoke the key or change the password first. Once it has been pushed, treat it as public, even after you clean the history.

Why this works

Removing a file in a new commit is not enough: every older commit still contains it. That is why the fix either rewrites the last commit (before pushing) or every commit (after pushing). But rewriting history cannot take back what has been public: clones, forks and bots that scan new commits for keys may already have it. Revoking the key is the real fix; the history cleanup only stops it from spreading further.