Move commits you made on the wrong branch
I committed to main instead of my feature branch — how do I move the commits?
Short answer
Create a branch at your current position to keep the commits, then reset the branch you polluted back to where it should be. Nothing is lost because the new branch holds the commits the whole time.
git branch feature/my-work
git reset --hard origin/main
git switch feature/my-work
Save the commits on a new branch, then rewind main
Does this match your situation?
- You committed to main, master or develop by accident.
- Work meant for a feature branch is sitting on the wrong one.
- You haven't pushed yet (or only your own branch is affected).
Step-by-step fix
Check what you're moving
Identify exactly which commits are on the wrong branch and where the branch should have stayed. Comparing against the remote makes the boundary obvious.
step 1git log --oneline -5 git log origin/main..HEAD --oneline # commits you added locallyCreate a branch at your current commit
This is the critical step and it's completely safe: it just adds a second pointer to the commit you're already on. Your work now exists on the correct branch before you touch anything else.
step 2git branch feature/my-workRewind the wrong branch
Reset the branch you were on back to where it should be. Since the commits are also on your new branch, nothing is lost.
step 3git reset --hard origin/main # match the remote exactly # or step back a known number of commits: git reset --hard HEAD~3If you have uncommitted work as well, stash it first — --hard discards it.
Switch to your feature branch and carry on
Everything is where it should be: main matches the remote, and your commits live on the feature branch ready to push.
step 4git switch feature/my-work git log --oneline -5 git push -u origin feature/my-workIf you only need to move some commits, cherry-pick
When just one or two commits belong elsewhere, copy them across individually and then drop them from the original branch.
step 5git switch feature/my-work git cherry-pick a1b2c3d git switch main git reset --hard HEAD~1Already pushed to main?
Don't rewrite a shared branch. Revert the commits on main instead, and cherry-pick them onto your feature branch so the work isn't lost.
step 6git revert a1b2c3d git push git switch feature/my-work git cherry-pick a1b2c3d
Why this works
Because a branch is only a pointer to a commit, 'moving commits between branches' really means pointing a different name at them. Creating a branch at HEAD costs nothing and instantly gives the commits a second home; resetting the original branch then moves only that pointer. The commits themselves never move, are never copied, and are never at risk — which is why this fix is completely safe as long as you create the new branch first.
How to stop it happening again
- Put the current branch in your shell prompt (starship, oh-my-posh, posh-git) so it's always visible.
- Protect main on the server so direct pushes are rejected.
- Get into the habit of git switch -c feature/x as the first step of any new task.
Commands used in this guide
git branchList, create, rename and delete branches.
git resetMove the current branch pointer, optionally rewriting the index and your files.
git switchMove to another branch - the modern, safer half of git checkout.
git cherry-pickCopy one specific commit onto your current branch.
git revertCreate a new commit that undoes an earlier one, leaving history intact.
Still stuck?
Search the full command reference and every other rescue guide — there are 56 of them, covering everything from detached HEAD to force-push disasters.
Browse all guides →Frequently asked questions
How do I move my last commit to a different branch?
git branch <new-branch> creates a pointer at the commit, then git reset --hard HEAD~1 rewinds the branch you were on, then git switch <new-branch>. The commit was never in danger because the new branch referenced it the whole time.
What if I already pushed to main?
Don't force-push a shared branch. Revert the commits on main so history stays valid for everyone, then cherry-pick the original commits onto your feature branch so the work continues where it belongs.
Can I move commits without losing uncommitted changes?
Yes — stash them first with git stash -u, do the branch-and-reset, then git stash pop on the correct branch. git reset --hard would otherwise discard uncommitted work.
Related rescue guides
How do I undo the last commit but keep my work?
git reset --soft HEAD~1 removes the commit and leaves every change staged, ready to recommit. Use --mixed (the default) if you also want the changes unstaged, and only use --hard if you genuinely want the work destroyed. If the commit is already pushed, revert it instead of resetting.
Read the fix →Usually recoverableI deleted a Git branch — how do I get it back?
Deleting a branch removes a pointer, not the commits. Find the branch's last commit hash in the reflog and recreate the branch at that hash with git branch <name> <hash>. If the branch was pushed, the copy on the server may still exist and can simply be fetched back.
Read the fix →Always fixableHow do I undo a commit that's already on the remote?
On a shared branch, use git revert <hash> — it adds a new commit that reverses the change, so nobody else has to do anything. Rewriting with reset plus a force-push is only appropriate on a branch you're certain nobody else has pulled.
Read the fix →Always fixableHow do I combine multiple commits into a single commit?
git rebase -i HEAD~n opens an editor where you mark commits as squash or fixup to fold them into the one above. For a quick all-in-one collapse, git reset --soft HEAD~n followed by a fresh commit achieves the same result with no editor at all.
Read the fix →