Q: You started coding directly on `main` and made three commits. You realize this work belongs on a feature branch and you haven't pushed yet. How do you move those commits to a new branch and clean up `main`?
A branch in Git is just a pointer to a commit, so "moving" commits is really just moving pointers around.
Want to master this scenario in a live sandbox? KodeKloud's Advanced Git Workflows, Rebase & Interactive Diagnostics covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: "In a fast-paced team with dozens of pull requests merged daily, Git workflow hygiene was critical. The interviewer is testing: Basic branch mechanics, understanding that branches are just movable pointers to commits.. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third."
Initial Diagnostics & Root Cause Analysis
A branch in Git is just a pointer to a commit, so "moving" commits is really just moving pointers around.
- Create the feature branch from where you are right now — this captures your three commits on the new branch:
- Reset
mainback to where the remote is — your localmainstill points at the third commit; rewind it: - Switch to the feature branch and continue:
Remediation & Permanent Safeguards
The order matters: create the new branch *before* you reset, otherwise the commits become unreachable from any branch (they'd still be in reflog for ~90 days, but don't rely on that). Since you never pushed, no one else is affected.
git branch feature/my-work
- Create the feature branch from where you are right now — this captures your three commits on the ...
- Reset main back to where the remote is — your local main still points at the third commit; rewind...
- Switch to the feature branch and continue:
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
-
Running raw git push --force rather than git push --force-with-lease on shared team feature branches when configuring started coding directly
git push --forceunconditionally overwrites remote commits made by teammates, whereas--force-with-leaseaborts if the remote ref was updated. - Resolving complex merge conflicts with blind checkout overwrite strategies without running unit tests when configuring started coding directly Unilateral conflict resolution frequently clobbers complementary logic introduced in parallel feature branches, creating regression bugs.
-
Discarding corrupted working directories prematurely instead of recovering lost state using git reflog when configuring started coding directly
git reflogtracks every HEAD movement, allowing instant recovery of discarded commits, botched rebases, or deleted local branches. - Disabling branch protection rules and mandatory PR approval reviews on shared release branches when configuring started coding directly Without branch protection, developers can bypass CI validation gates and push untested code directly to production branches.