DEV Community

miku86
miku86

Posted on • Edited 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.

Logic:

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

Step-by-Step:

  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)

Collapse
 
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.

Collapse
 
miku86 profile image
miku86

@mygeen ,

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

Collapse
 
greatbahram profile image
Bahram Aghaei

Creative alternative.

Collapse
 
atilafullstack profile image
Atila Augusto 🛡️

And if i do that in remote?