Interview Prep/Git & GitHub/Undoing Mistakes

You committed `.env` earlier and want Git to stop tracking it but keep the local copy.

BeginnerScenarioUndoing Mistakes
Answer

Situation read → .gitignore only affects untracked files, so a tracked file must be removed from the index.

What I'd do →

  1. git rm --cached .env.
  2. Add .env to .gitignore.
  3. Commit: git commit -m "Stop tracking .env".
  4. If it was pushed, treat contents as leaked: rotate every secret in it.

Why → Removing from the index does not remove it from history. Old commits still hold the file.

Interview tip → Rotation, not deletion, is what actually fixes a leaked secret.

All Git & GitHub questions Roadmap: Git & GitHub

More on Undoing Mistakes