简体   繁体   中英

git rebase/merge/cherry-pick a commit that rearranges a block of code

Suppose a master branch and a feature branch have the same piece of code originally:

Block A
Block B

where Block A and Block B can be arbitrary lines of code. Now, in the master branch, Block A is changed, eg

**Block A**
Block B

And then, in the feature branch, Block A is moved after Block B, ie

Block B
Block A

What git think I'm doing is deleting Block A and adding some new lines after Block B, so if I try to

  1. Merge feature branch into master ,
  2. Rebase feature branch on master , or
  3. Cherry-pick feature in the master branch,

a conflict happens:

<<<<<<< HEAD
**Block A**
=======
>>>>>>> Move Block A behind Block B
Block B
Block A

A conflict is expected, but the problem here is, git cannot help update the changes in Block A. Is there a better solution to this situation? For example, somehow make git think that I am deleting Block B and adding stuff before Block A?

(I would welcome any suggestions for a better title.)

There is no better way, at the moment, to deal with this than to resolve the conflict at merge time: simply pick the new Block A code and the updated Block B code from the two "sides" of the merge. (This may be easier if you set merge.conflictStyle to diff3 so that you can see, in the conflicted file, how the code was laid out in the merge base version. If you use various merge tools, this may not make any difference, so whether to set merge.conflictStyle to diff3 is up to you.)

A future, smarter diff and merge algorithm might be able to figure this out and give you a simpler merge conflict to resolve, or even resolve it on its own. But that's not available in Git today.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM