diwhyyyyy
diwhyyyyy

Reputation: 6382

Amalgamate two merges in Git

I have a Git branch which is a topic branch that shows the project master. I merged it with the master and resolved a conflict manually. After that, a critical bug was fixed in master and I merged again, this time without conflict. I did not commit anything myself between the two merges and I have not pushed the branch to anyone else. I would like to combine the two merges.

What I have now is:

master ----A---B-----C---------->
                \     \
topic   ---W-----X-----Y-------->

Where:

I would like to make this into:

master  ----A---B---C----------->
                     \
topic   ----W---------X'-------->

Where X' is now a merge incorporating the conflict resolution and the bugfix in a single merge commit.

I know I could do it manually by checking out W and merging master, and copying the contents of the resolved files from X during the inevitable conflict. Is there a quicker way with less chance of messing it up (in the case that the conflict was large and ugly)?

Upvotes: 1

Views: 44

Answers (3)

UpAndAdam
UpAndAdam

Reputation: 5477

Pretty sure you could just do git rebase origin/master here..

Another point is that branches are cheap. Why not create a seperate clone of your topic say topic' and then manipulate him, at the end you can do a diff between it and topic to show yourself that you haven't changed anything (only commit history has changed)

Upvotes: 0

twalberg
twalberg

Reputation: 62519

If you truly haven't pushed anything since at least W, then this is probably the easiest way (outside of the actual conflict resolution - which may be painful, I guess) to accomplish it:

git checkout topic
git reset --hard W
git merge master

If you have pushed since W, though (in particular if either X or Y exist anywhere outside your repository), I'm not sure there is a clean solution to this - no matter what you do, you'll cause problems for any other repositories that have either X or Y in them.

Of course, you'll have to re-do the conflict resolution, unless you have rerere enabled.

Upvotes: 1

VonC
VonC

Reputation: 1330102

Quicker?

git checkout topic
git rebase -i W

You can then squash the last two commits.

git merge -ours master

(to record again the merge between master and topic, without having to "redo" the merge)

You could also try with the -p (--preserve-merge) option, to preserve those merge links between master and topic

git rebase -p -i W

Upvotes: 1

Related Questions