I have the following situation:
A---B---F---G---H (master)
\
\
C---D---E (experimental)
My problem is that B is a very-very bad thing that shouldn't have happened on master
. It belongs on experimental
. However F---G---H are okay. Is there a way to make everything look like this:
A---F'---G'---H' (master)
\
\
B---C---D---E (experiment)
I've read about rebase and stuff like that but the biggest problem is that master
has been pushed to origin
.
We can create a new branch with parent master branch and use git cherry-pick command to move each commit from one branch to another. This solution is OK, when you don't have many commits, because for each commit you need to do git cherry-pick .
After a pull request is opened, you can change the base branch to compare the changes in the pull request against a different branch.
Rebase is one of two Git utilities that specializes in integrating changes from one branch onto another. The other change integration utility is git merge . Merge is always a forward moving change record. Alternatively, rebase has powerful history rewriting features.
On master, run:
git revert B
You can then push safely, if the changes downstream of that commit are not impacted by it directly (i.e. it the contents of the commit B
can be removed without upsetting the other changes that came after it. It certainly sounds like this is the case.)
This would create:
A---B---F---G---H---I (master)
\
\
C---D---E (experimental)
Where I
is the revert commit. Master keeps its history whilst removing the contents of B
, experimental retains B
's changes.
You may have to revert commit I
if you later merge experimental into master.
git rebase --onto A B master
will do.
Seems you have master already pushed to orgin, if you are certain that it is safe to overwrite master branch of the origin, just do a git push -f
on master branch. Be aware that it may cause other developers have a conflict when they pull from origin.
Generally, branch in public repo should stay untouched, which means you can not expect removing commit B from the master, all you can do is fixing the mistake introduced by commit B in a new commit and push it to master again.
If it is possiable to inform your team members about the rebase and you insist to do so, then it is okay for a rewrite, just make sure this won't happer too often.
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