Jeti
Jeti

Reputation: 246

Changing the parent of a branch

Been looking for a solution for a while now, and since I'm far from an expert on git - even if I came close to the solution I couldn't modify it slightly to get it done.

I've been trying to work work like suggested here: http://nvie.com/posts/a-successful-git-branching-model/, but due to the hurry and what not while branching off for a hotfix - i branched off from dev instead of master.

Simplified, lets say we have a following history:

A---B---C--------D---- < developer
\      / \      /
 R1--R2   H1---H2
       \         \
--------X---------Y--- < master

R1 and R2 are relase branch commits and are fine. However, H1 and H2 are hotfix branch commits and H1 should have branched of from X (master), not C (dev). Since both X and C commits have the same code behind them, is it possible to have H1 branch from X instead?

I'd like to have it look like this:

A---B---C--------D---- < developer
\      /        /
 R1--R2   H1---H2
       \  /      \
--------X---------Y--- < master

This doesn't change code at all, i'd just like to be consistent and have branches starting where they should. Thing is - there are few more commits later on on both dev and master branch. H1-H2 branch is basically over a month old, with over a hundred commits later on.

As i noted, i'm a git beginner and been playing with rebase and what not, but that ended horribly wrong.

Any cure for this?

Upvotes: 3

Views: 96

Answers (1)

poke
poke

Reputation: 387647

You cannot do that without rewriting H1, then in turn rewriting H2, which also means rewriting both D and Y. So all the commits need to be changed and republished. And republishing already published commits is generally a bad idea. So if it’s not a too big problem, you should not do it and just keep it as it is.

If you still want to do it, you could do it as follows:

  • Create a new (temporary) branch based on X.
  • Cherry pick H1; the resulting H1' is now based on X.
  • Cherry pick H2.
  • Reset master to X (git reset --hard X).
  • Merge the temporary branch into master.
  • Delete the temporary branch.
  • Reset developer to C.
  • Merge master into developer.
  • Publish both branches using the force option.

Upvotes: 2

Related Questions