loading...

Git: How to move misallocated commits

miku86 profile image miku86 Updated on ・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.

Discussion

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.