medna13
medna13

Reputation: 21

Is it possible to "unmerge" a merge commit by simply removing one of the parent pointers?

I would like to "unmerge" branch "A" from branch "B".

https://i.sstatic.net/y4GOu.png

In the above image, that would mean simply removing the dotted line between commit "095195e" and "A".

I do not want to use revert, because I may later merge branch "B" back into branch "A" and I would have to revert the revert.

Also, say these two branches continue on, and A is merged into B several more times. Would there be a way to use this "unmerge" operation to remove all merges of A into B.

Thanks

Upvotes: 2

Views: 194

Answers (2)

A.H.
A.H.

Reputation: 66263

There are two reasons why this does not work:

1) The parent refs go into the calculation of the commit's hashcode. So you would Rewrite the history which is -- usually not recommended for public branches.

2) Linus once said, that Git is a stupid content tracker. This means: Git stores snapshots, not differences. In your case this means that just removing the second parent pointer would not "undo" the changes of the files associated with the merge commit.

Upvotes: 0

eftshift0
eftshift0

Reputation: 30184

Can you just remove a parent from a revision? Nope. It would require a rewrite of the history of branch B:

git checkout e137e9b # go back to the revision before A was merged into B
git cherry-pick B~2..B # apply all changes past the point where the branches where merged
# if everything looks fine, move B pointer
git branch -f B
git checkout B

Now A and B are separate

The same thing can be achieved with rebase:

git rebase --onto e137e9b B~2 B

But it's not as useful in terms of explaining what's going on (exactly the same that I did on my first recipe).

Upvotes: 3

Related Questions