You want to discard local edits to one file and go back to the last commit.
BeginnerScenarioUndoing Mistakes
Answer
Situation read → Working tree changes are unwanted.
What I'd do →
git restore path/to/file, orgit restore .for everything.- For untracked files, preview first:
git clean -nd, thengit clean -fd.
Why → These changes were never committed, so this cannot be undone. Hence preview with -n.
Interview tip → Mention that git stash is the non-destructive alternative if you are unsure.
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