packoman
packoman

Reputation: 1282

How to rebase a git branch that contains a merge commit *and* corresponding, previous branch-point

I am trying to interactively rebase commits from the branch tmp/20241220-rebase-source onto tmp/20241220-rebase-target. The commits to rebase include a branching point and corresponding merge-commit (ba87116b). This is how tmp/20241220-rebase-source looks like:

* 1df0fff2
* f2659d53
*   ba87116b  <-- I believe this commit contained merge conflicts, which were fixed manually; but I want to rebase the commit as is
|\  
| * decacca4
* | 22820e76
|/  
* 212d1db1
* 304733f0
* e7d2ed57
* 78fd41bf
* 15251fab
* 6854d87c
* 53d2b1c8
* fbf44673
* 911afc16
* bb56930f
* 75d7085d
* 23674b1a <- commit is already cherry-picked into `tmp/20241220-rebase-target`, to fix a merge conflict (the following commits do to touch this file and thus there cannot be a merge conflict) 
| <- other commits to ignore from rebase, below 
|

I cherry-picked the first commit (23674b1a), because it caused a merge-conflict in a single file, which I fixed manually. All other commits /add/ new files and hence should not produce merge conflicts.

git checkout tmp/20241220-rebase-source
git rebase --rebase-merges --onto tmp/20241220-rebase-target --interactive 23674b1a

Running the rebase script results in merge conflicts for the merge commit ba87116b:

(tmp/20241220-rebase-source)> git rebase --rebase-merges --onto tmp/20241220-rebase-target --interactive 23674b1af2c7d4451ba5a5b788e91fcaf9d3e90d

Auto-merging example_form_xml/form1/file1.xml
CONFLICT (content): Merge conflict in example_form_xml/form1/file1.xml
Auto-merging example_form_xml/form1/file2.xml
CONFLICT (add/add): Merge conflict in example_form_xml/form1/file2.xml
Could not apply ba87116b...

I belive this this issue is caused, because the original merge commit ba87116b contained conflicts, which were resolved manually and that I have resolve them again in the rebase. But this is not my desired behavior, since I just want to rebase the commit as is.

Is this possible and if so, how do I achieve this?

Upvotes: 1

Views: 38

Answers (1)

eftshift0
eftshift0

Reputation: 30297

This is not the most elegant approach to solving it but it will allow you to move forward. If you think about it, ba87116b is just what code would look like if you had developed decacca4 on top of 22820e76 (or viceversa) instead of 212d1db1. Being that the case, I would straighten that branch before I tried the rebase:

git rebase 212d1db1 1df0fff2

Use the right branch name as the second commit id over there.

What should happen when you run this rebase is that it should stop on conflicts when applying either 22820e76 or decacca4 (whichever was applied second). When that happens, just take advantage of the tree in ba87116b as that is the resulting code that should be there, no need to redo the conflicts by hand:

git restore --source=ba87116b --worktree --staged -- .
git rebase --continue

And that should allow the rebase to finish.... and now you can rebase the branch as it is a straight line and you have incorporated the previous conflict resolution in your straightened branch.

Upvotes: 3

Related Questions