You committed on `main` when you meant to be on a feature branch. Nothing is pushed. Fix it.
BeginnerScenarioUndoing Mistakes
Answer
Situation read → The commit exists locally only, so history can be moved freely.
What I'd do →
- Create the branch at the current commit:
git branch feature/login. - Move
mainback:git reset --hard origin/main(orHEAD~1for one commit). git switch feature/login; your commit is intact.
Why → Branches are just pointers. Creating a new pointer first means the reset cannot lose anything.
Interview tip → Always create the safety branch before running reset --hard.
More on Undoing Mistakes
You 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.BeginnerAfter `git stash pop` you got conflicts. Is your stash lost?Intermediate