【问题标题】:How do I make Git always use "theirs/ours" for resolving conflicts in a particular file, during rebase?如何让 Git 在变基期间始终使用“他们的/我们的”来解决特定文件中的冲突?
【发布时间】:2015-07-22 10:16:37
【问题描述】:

我从一个非常活跃的存储库创建了我自己的dev 分支。此存储库 (Clappr) 还包含一个已编译和缩小的文件,该文件已使用源代码进行更新。

每当我想用master 重新定义我的dev 分支时,这个文件就会发生冲突,因为它不能自动合并——当然,因为它是一个缩小的JS 文件:

$ git checkout dev
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: dummy commit
Using index info to reconstruct a base tree...
M   dist/clappr.js
M   src/playbacks/hls/hls.js
Falling back to patching base and 3-way merge...
Auto-merging src/playbacks/hls/hls.js
Auto-merging dist/clappr.js
CONFLICT (content): Merge conflict in dist/clappr.js

我可以使用--ours 解决这个冲突,但是我必须为master 上的每一个我还没有变基的提交都这样做。

我知道我可以完全跳过一个补丁,但我能以某种方式自动告诉 Git 忽略该 clappr.js 文件并始终使用我的 dev 分支中的任何内容吗?

【问题讨论】:

    标签: git git-rebase


    【解决方案1】:

    您可以定义一个自定义合并驱动程序(具体示例如“Git: How to rebase many branches (with the same base commit) at once?”)。

    dist/clappr.js 关联的合并驱动程序“keepMine”,并在每个分支中存在的.gitattributes 文件中声明该合并驱动程序。
    见“How do I tell git to always select my local version for conflicted merges on a specific file?”。

    echo clappr.js merge=keepMine > dist/.gitattributes
    git config merge.keepMine.name "always keep mine during merge"
    git config merge.keepMine.driver "keepMine.sh %O %A %B"
    

    keepMine.sh 放在$PATH 的某个位置,而不是存储库中:

    # I want to keep MY version when there is a conflict
    # Nothing to do: %A (the second parameter) already contains my version
    # Just indicate the merge has been successfully "resolved" with the exit status
    exit 0
    

    (对于KeepTheir.sh,请参阅“How to stop a merge (yet again…)”)


    更简单,正如branch14the comments 中所指出的:

    Linux 已经有一个成功地什么都不做的命令,恰当地命名为“true”。

    git config merge.keepMine.driver "true"
    

    【讨论】:

    • Linux 已经有一个成功地什么都不做的命令,恰当地命名为“true”。不需要keepMine.sh,使用git config merge.keepMine.driver "true"即可。
    • @branch14 谢谢。当我 5 年前写这篇文章时,这一定是我的想法。我已经编辑了答案以包含您的评论,以提高知名度。
    猜你喜欢
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 2011-01-01
    • 2014-08-29
    相关资源
    最近更新 更多