threads / discuss / 27058

Converting merge to rebase in the presence of conflicts

Subject: Converting merge to rebase in the presence of conflicts

## tl;dr

6 messages between Apr 11, 2011 and Apr 12, 2011.

replies: 5people: 4as markdown or json

Hrvoje Nikšić· Apr 11, 2011, 14:13 UTC · lore
Is it possible to convert a merge with (resolved) conflicts into a rebase?

I find it useful to always pull commits with "git pull" because it is less intrusive on my local commits (if any) than "git pull --rebase", and it provides a nicer change summary. Also, conflict resolution seems more intuitive when merging than when rebasing. And If I decide later to rebase after all, I can simply issue "git rebase origin/bla". As long as there are no conflicts.

If the merge resolves conflicts, the subsequent rebase is unaware of that, and the conflicts must be resolved once more. Here is an example in a single repository that just uses 'merge' to show the scenario:

# create a common file with two lines and branch off printf 'foo\nbar\n' > a git add a && git commit -m '' a git add a && git commit -m 'Added.' a git branch somebranch

# now work on the file in both branches echo baz >> a git commit -m changed a git checkout somebranch echo qux >> a git commit -m changed2 a

# we're on somebranch and decide to merge our changes with master: git merge master vi a # resolve conflicts git add a git commit # commit the merge

At this point say the developer on somebranch changes his mind and wants to rebase after all. The result is, again, a conflict:

# try to change merge to rebase git rebase master [... long output on conflicts ...]

First, rewinding head to replay your work on top of it...
Applying: changed2
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging a
CONFLICT (content): Merge conflict in a
Failed to merge in the changes.
Patch failed at 0001 changed2

When you have resolved this problem run "git rebase --continue". If you would prefer to skip this patch, instead run "git rebase --skip". To restore the original branch and stop rebasing run "git rebase --abort".

Is there a way to rebase the branch without redoing conflict resolution? Some option to rebase, or a trick with rebase -i? The conflict resolution must obviously go into some commit, it would be nice to be able to choose whether to amend an existing commit with the additional changes or to add a new commit.

Hrvoje
Michael J Gruber· Apr 11, 2011, 14:39 UTC · re: Hrvoje Nikšić · lore

Re: Converting merge to rebase in the presence of conflicts

Hrvoje Nikšić venit, vidit, dixit 11.04.2011 16:13:
Show 60 quoted lines
> Is it possible to convert a merge with (resolved) conflicts into a rebase?
> 
> I find it useful to always pull commits with "git pull" because it is
> less intrusive on my local commits (if any) than "git pull --rebase",
> and it provides a nicer change summary. Also, conflict resolution
> seems more intuitive when merging than when rebasing. And If I decide
> later to rebase after all, I can simply issue "git rebase origin/bla".
> As long as there are no conflicts.
> 
> If the merge resolves conflicts, the subsequent rebase is unaware of
> that, and the conflicts must be resolved once more. Here is an example
> in a single repository that just uses 'merge' to show the scenario:
> 
> # create a common file with two lines and branch off
> printf 'foo\nbar\n' > a
> git add a && git commit -m '' a
> git add a && git commit -m 'Added.' a
> git branch somebranch
> 
> # now work on the file in both branches
> echo baz >> a
> git commit -m changed a
> git checkout somebranch
> echo qux >> a
> git commit -m changed2 a
> 
> # we're on somebranch and decide to merge our changes with master:
> git merge master
> vi a           # resolve conflicts
> git add a
> git commit     # commit the merge
> 
> At this point say the developer on somebranch changes his mind and
> wants to rebase after all. The result is, again, a conflict:
> 
> # try to change merge to rebase
> git rebase master
> [... long output on conflicts ...]
> 
> First, rewinding head to replay your work on top of it...
> Applying: changed2
> Using index info to reconstruct a base tree...
> Falling back to patching base and 3-way merge...
> Auto-merging a
> CONFLICT (content): Merge conflict in a
> Failed to merge in the changes.
> Patch failed at 0001 changed2
> 
> When you have resolved this problem run "git rebase --continue".
> If you would prefer to skip this patch, instead run "git rebase --skip".
> To restore the original branch and stop rebasing run "git rebase --abort".
> 
> 
> Is there a way to rebase the branch without redoing conflict
> resolution? Some option to rebase, or a trick with rebase -i? The
> conflict resolution must obviously go into some commit, it would be
> nice to be able to choose whether to amend an existing commit with the
> additional changes or to add a new commit.
> 
> Hrvoje

If you enable rerere before the merge then the merge resolution is recorded and can be reused by rebase. It still stops but applied the previous resolution automatically already.

Michael
Johannes Sixt· Apr 11, 2011, 15:03 UTC · re: Hrvoje Nikšić · lore

Re: Converting merge to rebase in the presence of conflicts

Am 4/11/2011 16:13, schrieb Hrvoje Nikšić:
> Is there a way to rebase the branch without redoing conflict
> resolution?
git config --global rerere.enabled true
-- Hannes
Hrvoje Nikšić· Apr 11, 2011, 15:15 UTC · re: Johannes Sixt · lore

Re: Converting merge to rebase in the presence of conflicts

Thanks for the responses, rerere works nicely, if somewhat cryptic. At the rebase step I got:

First, rewinding head to replay your work on top of it...
Applying: changed2
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging a
CONFLICT (content): Merge conflict in a
Resolved 'a' using previous resolution.
Failed to merge in the changes.
Patch failed at 0001 changed2

When you have resolved this problem run "git rebase --continue". If you would prefer to skip this patch, instead run "git rebase --skip". To restore the original branch and stop rebasing run "git rebase --abort".

That's a lot of output, some of it scary-looking. (OK, maybe not
scary-looking to me, but certainly so to someone not yet fully
comfortable with git.)  Running "git rebase --continue" told me I
needed to "edit all merge conflicts and then mark them as resolved",
whereas the conflicts are in fact already resolved, but rebase doesn't
know it.  Again, this isn't a big problem for me, but it's unfriendly.
 "git add" followed by "git rebase --continue" worked exactly as I
hoped it would.

I still wonder why rerere is necessary here. After all, even without the rerere metadata, the information about conflict resolution is right there, in the merge commit, and rebase could conceivably make use of it. What am I missing?

Hrvoje
Michael Haggerty· Apr 12, 2011, 02:55 UTC · re: Hrvoje Nikšić · lore

Re: Converting merge to rebase in the presence of conflicts

On 04/11/2011 05:15 PM, Hrvoje Nikšić wrote:
Show 5 quoted lines
> [...]
> I still wonder why rerere is necessary here. After all, even without
> the rerere metadata, the information about conflict resolution is
> right there, in the merge commit, and rebase could conceivably make
> use of it. What am I missing?

In general, merge and rebase are quite different animals. In the example you provided, there was only one conflicting commit being rebased, so the difference is not so apparent.

One commit merged:
*--*--a--m      "master"
    \   /
     b--
One commit rebased:
*--*--a                           *--*--a--b'        "master"
    \              ->
     b

In either case, "git diff a..master" is identical. Therefore, converting "one commit merged" into "one commit rebased" simply involves forgetting the second parent of commit "m". The "rerere" facility will indeed help you here, as other people have suggested.

But if more than one commit is being brought in from the branch, then merge and rebase are more distinct. Merge forces you to resolve the conflicts only once, in a single merge commit, whereas rebase forces you to resolve the conflicts one commit at a time.

Multiple commits merged:
*--*--a---M      "master"
    \    /
     b--c
Multiple commits rebased:
*--*--a                          *--*--a--b'--c'     "master"
    \              ->
     b--c

It should still be that "git diff a..master" gives identical results in the two cases, but there is still a big difference--in the rebase example, master after commit "b'" should be a functional state that can be compiled and passes the unit tests. The state at commit "b'" includes a combination of the changes made in "a" plus the changes made in the original "b". In the merge example, there is no state of the tree that is equivalent; there is only a+b+c. This lack of intermediate conflict resolution is what makes merges problematic for "git bisect".

Now, you want a way to transform the merge into a rebase automatically.
 In other words, given the information in the "multiple commits merged"
example as raw material, how can you convert it into a rebase?  This is
only possible if you are willing to squash "b", "c", and "M" into a
single commit:
*--*--a--bcM

because you have only told git how to resolve the conflicts between "a" and "b+c", not between "a" and "b" without "c".

This is why I have advocated "rebase with history" [1], which retains both the intermediate conflict resolutions and also the merge information:

*--*--a--b'--c'     "master"
    \   /   /
     --b---c
Michael

[1] http://softwareswirl.blogspot.com/2009/04/truce-in-merge-vs-rebase-war.html

-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/
Johannes Sixt· Apr 12, 2011, 06:11 UTC · re: Hrvoje Nikšić · lore

Re: Converting merge to rebase in the presence of conflicts

Please don't cull Cc list; use "Reply to all" in your MUA.
Am 4/11/2011 17:15, schrieb Hrvoje Nikšić:
> I still wonder why rerere is necessary here. After all, even without
> the rerere metadata, the information about conflict resolution is
> right there, in the merge commit, and rebase could conceivably make
> use of it. What am I missing?

The conflicts that you see during a merge may be attributed to several different commits of the merged branch. Therefore, when you rebase the branch, you will see different subsets of the conflicts for each rebased commit, and it is possible that you see conflicts that you did not see in the merge. How can you expect rebase to "know" how to resolve them?

-- Hannes

← back to recent threads