DEV Community


Posted on • Updated on

Git: How to move misallocated commits

Accidentally, I committed 6 times into master instead of creating a new branch.

One wrong commit wouldn't be painful, I would do a simple git reset --soft HEAD~1, stash and re-commit.

But how to solve this problem?

Easier than expected.


  1. Create a new branch based on master
  2. Remove the commits from master


  1. Go into master: git checkout master
  2. Create a new branch based on master: git branch my-new-branch
  3. (In master): git reset --hard HEAD~N, where N is the number of commits I want to remove, 6 in my case.

Top comments (4)

mygeen profile image
Mygeen • Edited

Alternatively you can use git reset --hard origin/master in step 3. This resets the status of the local repository to the state of remote.

miku86 profile image

@mygeen ,

awesome, thanks for this information.
If you want to use it, first check if your remote is at the proper state.

greatbahram profile image
Bahram Aghaei

Creative alternative.

atilafullstack profile image
Atila Augusto 🛡️

And if i do that in remote?