Move a commit from main to a new branch
Careful: rewrites history
Short answer
Run git branch <new-branch>, then git reset --keep HEAD~1, then git switch <new-branch>. The commit now lives only on the new branch.
Only before you push main. --keep refuses to run if it would overwrite uncommitted changes, so nothing unsaved is lost.
The commands, one by one
- 1
git branch muffinsMakes a new branch called
muffinspointing at your latest commit, so the commit is safe there. You stay onmain. - 2
git reset --keep HEAD~1Moves
mainback one commit, as if you'd never committed on it.--keepstops if you have uncommitted changes in the way. - 3
git switch muffinsMoves you to the new branch, where your commit is.
For your own repository: Pick your own branch name. If you made several commits on main, use HEAD~2, HEAD~3… to match.