告诉 git 在特定文件上使用我们的合并策略
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14093540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
tell git to use ours merge strategy on specific files
提问by Barak1731475
I have the following directory structure:
我有以下目录结构:
/.git
/.git/info/attributes
/MyProject
/MyProject/pom.xml
/MyProject/MyCode.java
I have branch master and bugfix. On both branches pom.xml and MyCode.java were modified. i would like to merge changes from bugfix to master only for MyCode.java and keep master version of pom.xml file.
我有分支主和错误修复。在两个分支上 pom.xml 和 MyCode.java 都被修改了。我想仅针对 MyCode.java 合并从 bugfix 到 master 的更改,并保留 pom.xml 文件的 master 版本。
So I added "/.git/info/attributes" because i don't want to commit .gitattributes with the project
所以我添加了“/.git/info/attributes”,因为我不想将 .gitattributes 与项目一起提交
$cat .git/info/attributes
pom.xml merge=ours
$git status
# On branch master
nothing to commit (working directory clean)
$git check-attr -a pom.xml
pom.xml: merge: ours
Finally to the problem:
最后解决问题:
When I do:
当我做:
$git merge bugfix
Auto-merging MyProject/pom.xml
CONFLICT (content): Merge conflict in MyProject/pom.xml
Automatic merge failed; fix conflicts and then commit the result.
Git is ignoring attributes setting. What am i missing here?
Git 忽略属性设置。我在这里错过了什么?
I prefer not to define "keepMine.sh" per this post
我不想在这篇文章中定义“keepMine.sh”
this postis what i need, however i prefer not to go file by file if i have a simple pattern
这篇文章是我所需要的,但是如果我有一个简单的模式,我不想逐个文件
thanks!
谢谢!
回答by jthill
$ git config merge.ours.driver true
or even
甚至
$ git config --global merge.ours.driver true
'ours' isn't one of the built-in merge drivers even though it's perfectly clear to you and me what it should do, and it seems git doesn't error out when a custom merge driver is undefined.
'ours' 不是内置合并驱动程序之一,即使您和我都非常清楚它应该做什么,而且当自定义合并驱动程序未定义时,git 似乎不会出错。
(true
above is just the unix true
command, its success says it made the local version look right, in this case by doing nothing to it.)
(true
上面只是 unixtrue
命令,它的成功表明它使本地版本看起来正确,在这种情况下,它什么都不做。)
回答by Tel
From my testing with git version 1.7.1 I find that unless there IS a requirement to merge, no merge driver will execute (regardless of what you put in the config, and regardless of any git attributes).
从我对 git 1.7.1 版的测试中,我发现除非有合并的要求,否则不会执行任何合并驱动程序(无论您在配置中放置什么,也无论任何 git 属性如何)。
If you branch out from the "master" to your own local branch "bugfix" and then edit a bunch of stuff in your own branch, commit that. Afterwards you checkout the "master" branch again, then do something like:
如果您从“master”分支到您自己的本地分支“bugfix”,然后在您自己的分支中编辑一堆内容,请提交。之后,您再次签出“master”分支,然后执行以下操作:
git merge bugfix
You might expect the "merge=ours" attribute to protect you in this situation. It won't! Branch "bugfix" will stomp over branch "master" if there have been no edits to that particular file within the master branch since the time "bugfix" branched away from master. No edits implies no need to merge which implies don't run a merge driver. This seems such a common problem that git should have a particular attribute pre-defined to give a file absolute local protection from foreign merges, under ALL situations.
在这种情况下,您可能希望“merge=ours”属性能够保护您。不会!如果自从“bugfix”从 master 分支出来后没有对 master 分支中的特定文件进行编辑,则分支“bugfix”将踩过分支“master”。没有编辑意味着不需要合并,这意味着不运行合并驱动程序。这似乎是一个普遍的问题,git 应该预先定义一个特定的属性,以便在所有情况下为文件提供绝对的本地保护以防止外部合并。
FWIW, I've read other people's opinions that config files should be local and private and not checked into git at all. I think that's kind of bollox: of course I do want to track changes in my config file. Of course the idea of a collaborative tool is that I want to be able to compare my config against what other people in my team are running, or compare the development config to the production config. The whole idea of a tool is that it helps you get your work done.
FWIW,我已经阅读了其他人的意见,即配置文件应该是本地和私有的,并且根本没有签入 git。我认为这有点麻烦:当然,我确实想跟踪我的配置文件中的更改。当然,协作工具的想法是我希望能够将我的配置与团队中其他人正在运行的配置进行比较,或者将开发配置与生产配置进行比较。工具的整体理念是帮助您完成工作。
As a kludgy solution, the best I've come up with is a config directory with each config file having a different name (i.e. config/production config/test config/devel_joe config/devel_bill and so on) and all of these go into git. Then at deployment the actual working config file is copied out of that config directory. I think it's an ugly approach because it is a step backwards away from the idea of named branches, but at least it is safe.
作为一个笨拙的解决方案,我想出的最好的方法是一个配置目录,每个配置文件都有不同的名称(即 config/production config/test config/devel_joe config/devel_bill 等等),所有这些都进入 git . 然后在部署时,实际工作的配置文件从该配置目录中复制出来。我认为这是一种丑陋的方法,因为它远离命名分支的想法,但至少它是安全的。