合并新代码的冲突解决方案

发布于 2024-12-28 22:50:35 字数 900 浏览 0 评论 0原文

假设我有一些已订购的代码,但订购不是技术要求。

apple
kiwi
strawberry

然后我有两个要合并的主题,其差异如下所示:

TOPIC BRANCH: orange
  kiwi
+ orange
  strawberry

还有是否

TOPIC BRANCH: pear
  kiwi
+ pear
  strawberry

有办法让这两个补丁自动解决?在我看来,这是一个合并冲突,因为它们竞争同一条新线。我提出的一个解决方案是重新排序其中一个更改,因为排序顺序只是一个软要求(其中水果实际上是函数定义)。

 TOPIC BRANCH: pear'
   apple
 + pear
   kiwi

现在我们可以将 orangepear' 合并在一起形成:

 _ apple
 p pear
 _ kiwi
 o orange
 _ strawberry

是否有其他方法可以解决这个问题,以便保持顺序?我还认为 pear 必须位于 orange 的下游,这样 orange 始终获得优先级,并且不会再出现合并冲突。但这是错误的依赖关系,因为 orangepear 是两个独立的功能分支。

一个可以在另一个之前被主干化,但这并不能解决集成分支的问题。

编辑:我突然意识到可以保留两个大块头(我猜只是添加?)可能有两种合并策略,称为“我第一”和“你第一”,这样可以在两个之间以非交互方式解决不明确的排序分支机构。

Let's say I have some code that is ordered, but the ordering is not a technical requirement.

apple
kiwi
strawberry

And then I have two topics that I want to merge in, whose diffs look like:

TOPIC BRANCH: orange
  kiwi
+ orange
  strawberry

And also

TOPIC BRANCH: pear
  kiwi
+ pear
  strawberry

Is there a way for these two patches to get resolved automatically? It seems to me like it is a merge conflict since they compete for the same new line. A solution I've come up with is to reorder one of the changes since the sort order is only a soft requirement (where fruits are actually function definitions).

 TOPIC BRANCH: pear'
   apple
 + pear
   kiwi

So now we can merge orange and pear' together to form:

 _ apple
 p pear
 _ kiwi
 o orange
 _ strawberry

Are there other ways to resolve this such that the ordering can be kept? I also thought of pear having to downstream from orange such that orange always gets priority and there wouldn't be a merge conflict anymore. But this is a false dependency since orange and pear are two separate feature branches.

One could be mainlined into the trunk before the other but that doesn't address integration branches.

Edit: It just dawned at me for two hunks that could be kept (additions only I guess?) there could be two merge strategies called "me first" and "you first" such that an ambiguous ordering could be resolved non-interactively between two branches.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风月客 2025-01-04 22:50:35

基本方法是定义一个自定义合并工具,然后使用 git 属性功能告诉 git 对这些文件使用该自定义合并工具。

示例:

  1. 创建测试存储库:

    <前><代码>$ git init t
    $ cd t

  2. 定义一个名为 mymerge 的自定义合并工具:

    $ git config merge.mymerge.name "我的自定义合并工具"
    $ git config merge.mymerge.driver "cat '%A' '%B'|sort -u >'%A'.tmp && mv '%A'.tmp '%A'"
    

    上面的合并工具连接文件,对结果行进行排序,然后删除重复的行。如果您不想更改顺序,请将上面的命令替换为可以执行您想要的操作的自定义脚本。请参阅 git help attribute 了解更多信息。

  3. 告诉 git 您在合并存储库中任何名为 foo.txt 的文件时要使用 mymerge

    $ echo "foo.txt merge=mymerge" >.gitattributes
    $ git add .gitattributes
    $ git commit -m "告诉 git 使用 mymerge 合并工具来合并 foo.txt"
    
  4. make some test data on三个分支:

    $ printf 'apple\nkiwi\nstrawberry\n' >foo.txt
    $ git add foo.txt
    $ git commit -m “foo.txt 的共同祖先版本”
    $ git checkout -b 橙色
    $ printf '苹果\nkiwi\norange\nstrawberry\n' > foo.txt
    $ git commit -a -m“添加橙色”
    $ git checkout -b pear master
    $ printf '苹果\nkiwi\n梨\n草莓\n' > foo.txt
    $ git commit -a -m“添加梨”
    
  5. 合并分支(注意没有冲突!):

    $ git checkout master
    $ git 合并橙色
    $ git 合并梨
    
  6. 利润!

    $ cat foo.txt
    苹果
    猕猴桃
    橙子
    梨
    草莓
    

The basic approach is to define a custom merge tool and then use the git attributes feature to tell git to use that custom merge tool for those files.

Example:

  1. create a test repository:

    $ git init t
    $ cd t
    
  2. define a custom merge tool called mymerge:

    $ git config merge.mymerge.name "my custom merge tool"
    $ git config merge.mymerge.driver "cat '%A' '%B'|sort -u >'%A'.tmp && mv '%A'.tmp '%A'"
    

    The above merge tool concatenates the files, sorts the resulting lines, and then removes duplicate lines. If you don't want to change the order, replace the above command with a custom script that does what you want. See git help attributes for more information.

  3. tell git that you want to use mymerge when merging any file named foo.txt in the repository:

    $ echo "foo.txt merge=mymerge" >.gitattributes
    $ git add .gitattributes
    $ git commit -m "tell git to use the mymerge merge tool for foo.txt"
    
  4. make some test data on three branches:

    $ printf 'apple\nkiwi\nstrawberry\n' >foo.txt
    $ git add foo.txt
    $ git commit -m "common ancestor version of foo.txt"
    $ git checkout -b orange
    $ printf 'apple\nkiwi\norange\nstrawberry\n' >foo.txt
    $ git commit -a -m "add orange"
    $ git checkout -b pear master
    $ printf 'apple\nkiwi\npear\nstrawberry\n' >foo.txt
    $ git commit -a -m "add pear"
    
  5. merge the branches (note no conflicts!):

    $ git checkout master
    $ git merge orange
    $ git merge pear
    
  6. profit!

    $ cat foo.txt
    apple
    kiwi
    orange
    pear
    strawberry
    
谷夏 2025-01-04 22:50:35

如果需要订购的代码位于单独的文件中,您可以使用自定义合并工具,该工具接受两个输入文件,读取它们并输出其内容,然后排序到结果文件中。然后,您将告诉 git 使用该文件的自定义合并工具。我对 git 的了解还不够,不知道您是否可以根据文件类型或某种正则表达式设置自定义合并工具,但这个想法可能会给您带来您想要的东西。

If the code that needs to be ordered is in a separate file, you could use a custom merge tool that takes two input files, reads them and outputs their contents, sorted into your result file. You would then tell git to use the custom merge tool for this file. I don't know enough about git to know if you can set custom merge tools based on file type or regex of some sort though, but it is an idea that might give you what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文