Code or Script
Move commits made on the wrong branch
Commits on the wrong branch are a bookkeeping problem, not data loss. The safe order is: mark the current state, create the branch you meant to use, then move the branch pointer back.
Stop doing this first
- Do not force-push a shared branch before creating the rescue branch.
- Do not reset before the feature branch exists.
The safe order of operations
- 1
Note the current SHA (git rev-parse HEAD) so you can always come back.
- 2
Create the feature branch at the current commit — the commits are now safely reachable from two places.
- 3
Move the wrong branch back to where it should be with git reset --hard <good-sha> (safe now that the branch exists).
- 4
If the commits were already pushed, prefer git revert or a cherry-pick + PR over force-pushing shared history.
Commands that do the work
Mark the current state
git rev-parse HEAD && git branch feature/rescuePut main back where it belongs
git checkout main && git reset --hard origin/mainMove a single commit instead
git checkout feature/rescue && git cherry-pick <sha>Realistic odds
Likely recoverable — this is almost always fully fixable.
Your case will differ in the details. Load this scenario into the recovery form and add what you already tried — you'll get a plan for your exact situation.
Get my recovery plan