Undo a changeAlways fixable

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.

quick fix
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

  1. 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 1
    git log --oneline -5
    git log origin/main..HEAD --oneline    # commits you added locally
    
  2. Create 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 2
    git branch feature/my-work
    
  3. Rewind 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 3
    git reset --hard origin/main    # match the remote exactly
    
    # or step back a known number of commits:
    git reset --hard HEAD~3
    

    If you have uncommitted work as well, stash it first — --hard discards it.

  4. 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 4
    git switch feature/my-work
    git log --oneline -5
    git push -u origin feature/my-work
    
  5. If 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 5
    git switch feature/my-work
    git cherry-pick a1b2c3d
    git switch main
    git reset --hard HEAD~1
    
  6. Already 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 6
    git 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 branch

List, create, rename and delete branches.

git reset

Move the current branch pointer, optionally rewriting the index and your files.

git switch

Move to another branch - the modern, safer half of git checkout.

git cherry-pick

Copy one specific commit onto your current branch.

git revert

Create 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

← All Git Rescue guides