All How-Tos
Intermediate

How to Move Commits to Another Branch in Git

Learn how to move commits made on the wrong branch to their intended branch.

Move to a New Branch

The examples start on main and move its latest local, unshared commit. They assume a regular commit with an earlier commit to return to. Replace the branch names as needed.

Your working tree and Index should be clean; commit or stash unfinished work first.

  • Create the Destination: The new branch retains the commit. --no-track prevents it from tracking the original branch.
git switch --no-track -c feature/correct
  • Move the Original Branch Back: While staying on feature/correct, point main at the preceding commit. Your current branch and working files remain unchanged.
git branch --force main HEAD~1

Move to an Existing Branch

As an alternative, start on main and choose an existing branch that does not yet contain the intended change.

  • Keep a Backup: Save the original tip under an unused branch name.
git branch --no-track backup/move
  • Copy the Commit: Switch to the destination and apply the saved commit there. Cherry-picking creates a commit on the destination branch.
git switch feature/existing
git cherry-pick backup/move
  • Move the Original Branch Back: Run this only after the cherry-pick completes successfully. The backup still preserves the original commit.
git branch --force main backup/move~1

Move Several Commits

For three latest commits in a linear history, use HEAD~3 when moving main back in the new-branch example.

For an existing destination, cherry-pick backup/move~3..backup/move to copy those three commits in order. After success, point main at backup/move~3.

Check the Result

Check that the destination contains your work and that main points to the intended earlier commit. Keep the backup until you have verified the copy.

git log --oneline --decorate --graph --all

Considerations

  • Creating a new branch at the original tip preserves the commit IDs. Cherry-picking onto a different history usually gives the copied commits new IDs.
  • When copying to an existing branch, the changes may depend on earlier source commits that the destination lacks. Test the result before moving main back, even if cherry-picking reports no conflicts.
  • If cherry-picking stops with conflicts, resolve the files, stage them with git add, and run git cherry-pick --continue. Use git cherry-pick --abort to cancel. Leave main in place until the copy succeeds.
  • If the source commits are already shared, coordinate a correction with collaborators. A follow-up revert commit can undo their changes while preserving the published history.
  • Remote branches keep their existing state until you explicitly push changes.
  • Git refuses to force-move main if it is checked out in another linked worktree. Finish the work in that checkout before moving its branch.