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 →
git rm --cached .env.- Add
.envto.gitignore. - Commit:
git commit -m "Stop tracking .env". - 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.
More on Undoing Mistakes
You committed on `main` when you meant to be on a feature branch. Nothing is pushed. Fix it.BeginnerYou made a typo in your last commit message and have not pushed.BeginnerA bad commit is already on shared `main`. Others have pulled it. How do you undo it?IntermediateYou ran `git reset --hard` and lost uncommitted-then-committed work. Can you get it back?IntermediateYou need to revert a merge commit on `main`. `git revert` says "commit is a merge but no -m option was given".IntermediateYou deleted a local branch with unmerged work by mistake.Beginner