Interview Prep/Git & GitHub/Undoing Mistakes

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 →

  1. Create the branch at the current commit: git branch feature/login.
  2. Move main back: git reset --hard origin/main (or HEAD~1 for one commit).
  3. 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.

All Git & GitHub questions Roadmap: Git & GitHub

More on Undoing Mistakes