ShareYourGroupShareYourGroupGit Help

Move a commit I made on the wrong branch

I committed on main but the work belongs on a new branch.

The problem

I committed on main but the work belongs on a new branch.

How the practice repository looked before

$ git status --short --branch
## main
$ git log --oneline -5
f4cff64 Add line two
20338b7 Add notes

The fix

$ git branch feature
(no output: that is normal)
$ git reset --hard HEAD~1
HEAD is now at 20338b7 Add notes

Check the result

$ git log --oneline main
20338b7 Add notes
$ git log --oneline feature
f4cff64 Add line two
20338b7 Add notes

Why this works

First make a new branch name at the current commit, so the commit is safe under that name. Then move main back one commit. The commit now lives only on feature.

If this is not quite your case

If you have more than one commit to move, use HEAD~2 and so on in the reset. If the commit is on the wrong branch and you want it on an existing one, switch to that branch and use git cherry-pick with the commit hash, then reset the wrong branch.

Be careful: Do this before you push main. If you already pushed, ask your team before rewriting it.

This is an independent guide. It is not connected with the Git project, GitHub, GitLab or any company named here. Every command below was run on 14 November 2026 with the real program (git version 2.56.0.windows.4) in a new empty practice repository; the output shown is what it printed. Facts about options were read in the official Git documentation on 14 November 2026.