git:如何添加自定义合并策略? [英] git: How do I add a custom merge strategy?

查看:24
本文介绍了git:如何添加自定义合并策略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加一个类似于这个问题中的自定义合并策略:Git 合并冲突总是取最新的文件

I'm trying to add a custom merge strategy similar to the one in this question: Git merge conflict to always take the newest file

我已将脚本保存为 git-merge-latest.sh 并将以下几行添加到 .git/config:

I've saved the script as git-merge-latest.sh and added the following lines to .git/config:

[merge "latest"]
    name = select latest file merge driver
    driver = git-merge-latest.sh %O %A %B

但是,当我运行 git pull --strategy latest 时,我收到消息:

However, when I run git pull --strategy latest, I get the message:

Could not find merge strategy 'latest'.
Available strategies are: octopus ours recursive resolve subtree.

我试过检查 git config merge.latest.driver,它返回预期的输出.我还将 driver 的值更改为 true 只是为了验证查找脚本没有问题.

I've tried checking git config merge.latest.driver, which returns the expected output. I also changed the value of driver to true just to verify that it wasn't a problem with finding the script.

这发生在运行 git 1.8.2.2 和 1.7.9.5 的两个不同系统上.我做错了什么?

This happens on two different systems running git 1.8.2.2 and 1.7.9.5. What am I doing wrong?

推荐答案

在这种情况下,您没有配置合并策略,而是配置了一个合并驱动程序:

In this case, you didn't configure a merge strategy, you configured a merge driver:

合并策略是确定如何合并两个(或多个)提交的程序.默认情况下,git merge 使用递归"策略,可以在 git-merge-recursive 程序中找到.通过为 git-merge(或 git-pull)指定 --strategy 标志,你告诉它调用不同的策略.如果你想插入你自己的合并策略,你可以通过在你的路径中创建一个可执行的 git-merge-mystrategy 并运行 git merge --strategy mystrategy.

A merge strategy is a program that determines how two (or more) commits are merged. By default, git merge uses the "recursive" strategy, found in the program git-merge-recursive. By specifying the --strategy <strategy> flag to git-merge (or git-pull) you tell it to invoke a different strategy. If you want to plug in your own merge strategy, you can, by creating an executable git-merge-mystrategy in your path and running git merge --strategy mystrategy.

这与合并驱动程序不同.合并驱动程序是用于解决合并两个提交时存在的文件冲突的机制.通过配置 merge.mydriver.driver 设置,您可以按照概述的方式插入自己的合并驱动程序.

This is different than a merge driver. A merge driver is the mechanism used to resolve a conflict on a file that exists when merging two commits. You plug in your own merge driver in the manner you outlined, by configuring a merge.mydriver.driver setting.

要为特定文件启用合并驱动程序,您需要在 .gitattributes 中为该文件配置驱动程序:

To enable your merge driver for a particular file, you need to configure the driver for that file in .gitattributes:

filename merge=mydriver

这篇关于git:如何添加自定义合并策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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