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

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

问题描述

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

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.

我搜索了一个答案:

  1. 我正在使用 --no-commit--no-ff 合并选项,但它不一样.
  2. 这里这里 有人问了同样的问题,但没有解决方案.
  3. 类似的情况似乎是 如何防止文件被合并 使用 .gitattributes 包含:somefile.php merge=ours.我试图找到一些会产生冲突或强制手动合并的合并选项,但到目前为止没有找到.
  4. .gitattributes 包含:somefile.php -merge 永远不会自动合并,因此强制手动合并.这是 90% 的解决方案,但我寻求的是尝试自动合并并将其标记为冲突,无论它是否成功.但这是迄今为止最接近解决方案的方法. (...感谢 Charles Bailey 的澄清...)
  5. 有人建议编写自定义合并驱动程序(12),但我对如何做到这一点尚不清楚.
  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.

变体 4. 描述

推荐答案

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

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.

首先,创建一个名为 merge-and-verify-driver 的合并驱动程序脚本.使其可执行并将其放在合适的位置(您可能需要考虑将此脚本检查到 repo 中,甚至,因为 repo 的配置文件将依赖于它).Git 将执行这个 shell 脚本来执行敏感文件的合并:

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

这只是执行 Git 本身通常所做的默认合并行为.关键区别在于脚本始终返回非零值(表示存在冲突,即使合并实际上已在没有冲突的情况下解决).

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).

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

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

在这个例子中,我将 merge-and-verify-driver 放在 repo 的顶级目录 (./) 中.您需要相应地指定脚本的路径.

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.

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

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

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

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天全站免登陆