在变基期间,如何使 Git 始终使用 "theirs/ours" 来解决特定文件中的冲突?

How do I make Git always use "theirs/ours" for resolving conflicts in a particular file, during rebase?

我已经从一个非常活跃的存储库创建了自己的 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: 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…)”)


更简单,如branch14 in 所述:

Linux already has a command that successfully does nothing, aptly named "true".

git config merge.keepMine.driver "true"