Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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.

like image 850
devmiles.com Avatar asked Aug 28 '12 15:08

devmiles.com


People also ask

How do I change parent branch branch?

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 .

Can I change the base branch in git?

After a pull request is opened, you can change the base branch to compare the changes in the pull request against a different branch.

What does a rebase do?

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.


2 Answers

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.

like image 143
bcmcfc Avatar answered Oct 14 '22 01:10

bcmcfc


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.

like image 40
weynhamz Avatar answered Oct 14 '22 01:10

weynhamz