You ran `git add .` and staged a file you didn't want to commit.
BeginnerScenarioUndoing Mistakes
Answer
Situation read → Only the index is wrong; the working file is fine.
What I'd do →
git restore --staged secrets.envunstages it and keeps your edits.- Add it to
.gitignoreif it must never be committed. git statusto confirm.
Why → restore --staged copies HEAD's version into the index. On older Git, git reset HEAD file does the same.
Interview tip → Review with git diff --staged before every commit.
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