devmiles.com
devmiles.com

Reputation: 10014

Started a branch too late, change the parent of my branch

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.

Upvotes: 5

Views: 1717

Answers (2)

weynhamz
weynhamz

Reputation: 2008

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.

Upvotes: 3

bcmcfc
bcmcfc

Reputation: 26755

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.

Upvotes: 5

Related Questions