Git: How to move misallocated commits

github logo ・1 min read

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.
twitter logo DISCUSS (4)
markdown guide
 

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.

 

@mygeen ,

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

 
 
Classic DEV Post from Apr 11

What's in your podcast rotation right now?

Curious what everyone is listening to for podcasts these days, whether it be te...

miku86 profile image
Developer & Explorer with a passion for learning, coding, futurology and technology. - Compare yourself to who you were yesterday, not to who someone else is today.

👋 Hey dev.to reader.

Do you prefer sans serif over serif?

You can change your font preferences in the "misc" section of your settings. ❤️