[英]How do I make Git always use "theirs/ours" for resolving conflicts in a particular file, during rebase?
I've created my own dev
branch from a pretty active repository.我从一个非常活跃的存储库创建了我自己的
dev
分支。 This repository (Clappr) also contains a compiled and minified file, which is updated with the source code.此存储库 (Clappr) 还包含一个已编译和缩小的文件,该文件已使用源代码进行更新。
Whenever I want to rebase my dev
branch with the master
, this file is conflicting, because it cannot be automatically merged—of course, because it's a minified JS file:每当我想用
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
I can resolve this conflict using --ours
, but I have to do this for every single commit on master
that I haven't rebased yet.我可以使用
--ours
解决这个冲突,但是我必须为我还没有重新定位的master
上的每一个提交都这样做。
I know I can skip a patch entirely, but can I somehow automatically tell Git to ignore that clappr.js
file and just always use whatever is in my dev
branch?我知道我可以完全跳过一个补丁,但是我可以以某种方式自动告诉 Git 忽略那个
clappr.js
文件并始终使用我的dev
分支中的任何内容吗?
You can define a custom merge driver (as in " Git: How to rebase many branches (with the same base commit) at once? " for a concrete example).您可以定义自定义合并驱动程序(如“ Git:如何一次重新设置多个分支(具有相同的基础提交)? ”中的具体示例)。
That merge driver " keepMine
" associated with dist/clappr.js
, and declare that merge driver in a .gitattributes
file present in each of your branches.该合并驱动程序“
keepMine
”与dist/clappr.js
关联,并在每个分支中存在的.gitattributes
文件中声明该合并驱动程序。
See " How do I tell git to always select my local version for conflicted merges on a specific file? ".请参阅“ 如何告诉 git 始终为特定文件上的冲突合并选择我的本地版本? ”。
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"
With keepMine.sh
put somewhere in your $PATH
, not in the repository:将
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
(For a KeepTheir.sh
, see " How to stop a merge (yet again…) ") (对于
KeepTheir.sh
,请参阅“如何停止合并(又一次……) ”)
Even simpler, as noted by branch14
in the comments :更简单,正如
branch14
在评论中指出的branch14
:
Linux already has a command that successfully does nothing, aptly named "
true
".Linux 已经有一个成功地什么都不做的命令,恰如其分地命名为“
true
”。
git config merge.keepMine.driver "true"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.