I made a series of commits after a good commit on the master branch, which in hindsight I should have made in another branch. Can I move those commits, beginning with a specific commit, to another branch, and keep the good commit as the last commit on master?
If you want to move commits to an existing branch you need to merge your changes into the existing branch before executing git reset --hard HEAD~3 (see Moving to an existing branch above). If you don't merge your changes first, they will be lost.
First, make sure that the target branch exists by running the “git branch” command. Now that you made sure that your branch exists, you can switch from the master branch to the “feature” branch by executing the “git checkout” command. That's it!
You can move commits from one branch to another branch if you want changes to be reflected on a different branch than the one to which you pushed the changes.
Sure:
$ git branch new-branch-name # Create a new branch from the current commit
$ git reset --hard <last good commit on master> # Reset master to the good commit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With