Git - 如何强制合并冲突和手动合并选定的文件 [英] Git - how to force merge conflict and manual merge on selected file

查看:151
本文介绍了Git - 如何强制合并冲突和手动合并选定的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们维护具有共同主分支和许多并行分支的Web应用程序,每个安装一个,每个安装都没有特定的变化。源代码在git中管理,当我们需要将主分支转移到并行转移功能和错误修正时,它是非常棒的工具。但很少有敏感和自动合并的文件通常会导致不好的结果。所以合并会容易得多,如果他们可以以某种方式标记,并且每一次合并都会导致需要手动合并的冲突。



我搜索了一个答案:


  1. 我正在使用 - no-commit - no-ff 合并选项,但它是不一样。

  2. 这里此处有人问相同问题,但没有解决方案。

  3. 类似的情况似乎是如何防止文件被合并使用.gitattributes包含: somefile.php merge = ours 。我试图找到一些合并选项,它会产生冲突或强制手动合并,但到目前为止没有发现任何合并选项。
  4. 自动合并,因此强制手动合并。这是90%的解决方案,但我寻求的是尝试自动合并,并将其标记为冲突,无论它是否成功。 但这距离解决方案非常近。 (...感谢Charles Bailey澄清......)
  5. 有人建议编写自定义合并驱动程序( 1 2 ),但如何做到这一点对我来说是远远不够的。

编辑:变体4.描述

解决方案

选项5是一个自定义合并驱动程序,可能是最接近您想要的方式。这很容易做到。下面是一个我认为应该让你非常接近你想要的行为的例子。

首先,创建一个名为 merge的合并驱动脚本-and-验证驱动器。将其设置为可执行文件并将其放在合适的位置(您可能需要考虑将此脚本复制到回购站中,因为回购站的配置文件将取决于它)。 Git将执行这个shell脚本来执行敏感文件的合并:

 #!/ bin / bash 
git merge-file$ {1}$ {2}$ {3}
exit 1

这只是Git自己通常所做的默认合并行为。主要区别在于脚本始终返回非零值(表明存在冲突,即使合并实际上已解决而没有冲突)。



接下来,您需要告诉Git你的自定义合并驱动程序的存在。您可以在repo的配置文件( .git / config )中执行此操作:

  [mergeverify] 
name =合并并验证驱动程序
driver = ./merge-and-verify-driver%A%O%B

在这个例子中,我在回购的顶部放置了 merge-and-verify-driver ./ )。您需要相应地指定脚本的路径。



现在,您只需为敏感文件提供适当的属性,以便在合并时使用自定义合并驱动程序这些文件。将此添加到 .gitattributes 文件中:

  *。sensitive merge =验证

在这里,我告诉Git名称与模式<$ c $匹配的任何文件c> *。sensitive 应该使用自定义合并驱动程序。显然,你需要使用适合你的文件的模式。


We maintain web application which has common master branch and many parallel branches, one for each installation, each have few specific changes. Source code is managed in git and it is terrific tool when we need transfer features and bugfixes from master branch into parallel ones. But are few files that are sensitive and automatic merging usually give bad results. So merging would be much easier if they can be somehow marked and every merge would result in conflict requiring manual merge.

I searched for an answer :

  1. I am using --no-commit and --no-ff merge options, but it is not the same.
  2. Here and here someone asks the same question but with no solution.
  3. Similar case seems to be how to prevent file being merged using .gitattributes containing: somefile.php merge=ours . I tried to find some merge option which would generate conflict or force manual merge but found none so far.
  4. .gitattributes containing: somefile.php -merge is never merged automatically and therefore forcing manual merge. It is 90% solution, but what I seek is to try automatic merge and mark it as conflict regardless it is successful or not. But this is so far closest to solution. (...thanks Charles Bailey for clarification...)
  5. Someone suggest to write custom merge driver (1, 2), but how to do it is far from clear to me.

edit: variant 4. description

解决方案

Option 5, a custom merge driver, is probably the way to get closest to what you want. It is surprisingly easy to do. Below is an example of one that I think should get you pretty close to the behavior you desire.

First, create a merge driver script called merge-and-verify-driver. Make it executable and put it in a suitable location (you may want to consider checking this script into the repo, even, since the repo's config file is going to depend on it). Git is going to execute this shell script to perform the merge of the sensitive files:

#!/bin/bash
git merge-file "${1}" "${2}" "${3}"
exit 1

This just does the default merge behavior that Git itself normally does. The key difference is that the script always returns non-zero (to indicate that there was a conflict, even if the merge was actually resolved without conflicts).

Next, you need to tell Git about the existence of your custom merge driver. You do this in the repo's config file (.git/config):

[merge "verify"]
        name = merge and verify driver
        driver = ./merge-and-verify-driver %A %O %B

In this example, I've put merge-and-verify-driver in the repo's top level directory (./). You will need to specify the path to the script accordingly.

Now, you just need to give the sensitive files the proper attributes so that the custom merge driver is used when merging those files. Add this to your .gitattributes file:

*.sensitive merge=verify

Here, I've told Git that any file with a name matching the pattern *.sensitive should use the custom merge driver. Obviously, you need to use pattern that is appropriate for your file(s).

这篇关于Git - 如何强制合并冲突和手动合并选定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆