Move a commit to another existing branch
Careful: rewrites history
Short answer
Switch to the right branch and git cherry-pick the commit, then switch back and git reset --keep HEAD~1 to remove it from the wrong branch.
Removing the commit from the wrong branch rewrites it: only do that part if you haven't pushed that branch.
The commands, one by one
- 1
git switch fixesGo to the branch the commit belongs on.
- 2
git cherry-pick mainCopies the latest commit of
mainontofixesas a new commit with the same changes and message. - 3
git switch mainBack to the branch that got the commit by mistake.
- 4
git reset --keep HEAD~1Takes
mainback one commit. The copy onfixesstays.
For your own repository: Instead of main, you can give the commit's hash from git log --oneline.