git .gitattributes & 单个文件的合并策略
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5465122/
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
.gitattributes & individual merge strategy for a file
提问by Ian
I have a master and a test branch of my (web) application. These projects are almost the same, except for one file which sets up the application, say "setup".
我的(网络)应用程序有一个主分支和一个测试分支。这些项目几乎相同,除了一个设置应用程序的文件,比如“setup”。
Whenever I merge one branch into the other, I would like that branch to keep its version of setup. That is, git should not attempt to merge the changes to that file.
每当我将一个分支合并到另一个分支时,我都希望该分支保持其设置版本。也就是说,git 不应该尝试合并对该文件的更改。
I followed the guidance from the Pro Git bookand created a .gitattributes file, with the line "setup merge=ours". However, this does not work - neither with fast forward merges not if I introduce conflicts.
我按照Pro Git 书中的指导创建了一个 .gitattributes 文件,其中包含一行“setup merge=ours”。但是,这不起作用 - 如果我引入冲突,那么快进合并也不行。
(To be precise:
(准确地说:
$: mkdir gitest
$: cd gittest
$: git init
$: echo "setup merge=ours" >> .gitattributes
$: echo "master" >> setup
$: git add setup .gitattributes
$: git commit -a -m ...
$: git branch test
$: git checkout test
$: echo "test" >> setup
$: git commit -a -m ...
$: git checkout master
$: git merge test
Expected result: setup contains the word "master", instead git performs a ff merge and setup is "test'.)
预期结果:setup 包含单词“master”,而不是 git 执行 ff 合并并且 setup 是“test”。)
回答by Jorge E. Cardona
I had the same error and it can be solved just defining an "ours" merge driver in .git/config:
我遇到了同样的错误,只需在 .git/config 中定义一个“我们的”合并驱动程序就可以解决这个问题:
[merge "ours"]
name = "Keep ours merge"
driver = true
Since true always return 0 the temporary file holding the current state will not be changed and will stay as the final version.
由于 true 始终返回 0,因此保存当前状态的临时文件不会更改,并将保留为最终版本。
You can read more about merge driver in here: http://www.kernel.org/pub/software/scm/git/docs/gitattributes.html#_defining_a_custom_merge_driver
您可以在此处阅读有关合并驱动程序的更多信息:http: //www.kernel.org/pub/software/scm/git/docs/gitattributes.html#_defining_a_custom_merge_driver
Addendum:
附录:
This works any time the driver is actually called and this seems to occur only when there are commits changing the same files (git the merge attribute). If there are changes in a single branch the driver is not going to be called.
这在实际调用驱动程序的任何时候都有效,并且这似乎仅在提交更改相同文件时发生(git 合并属性)。如果单个分支中有更改,则不会调用驱动程序。
回答by Kevin Bullaughey
I found that if I modified the files on both branches and committed the modifications to each branch, and then tried the merge, it would invoke the merge driver and listen to my .gitattributes
which specifies merge=ours
. After this, the two files always differ on the two branches and so the merge driver will always be invoked, so I didn't need to have a custom merge driver that touches the file. It was only necessary to have both modified initially.
我发现如果我修改了两个分支上的文件并将修改提交到每个分支,然后尝试合并,它会调用合并驱动程序并侦听我.gitattributes
指定的merge=ours
. 在此之后,这两个文件在两个分支上始终不同,因此将始终调用合并驱动程序,因此我不需要接触文件的自定义合并驱动程序。最初只需要对两者进行修改。
回答by Uwe Kleine-K?nig
The merge driver is only called in non-trivial cases, i.e. if both master and test have touched setup (and you need to define the merge driver ours
first):
合并驱动程序仅在非平凡情况下调用,即如果 master 和 test 都触及设置(并且您需要先定义合并驱动程序ours
):
git init
git config merge.ours.name '"always keep ours" merge driver'
git config merge.ours.driver 'touch %A'
echo "setup merge=ours" >> .gitattributes
echo "master" >> setup
git add setup .gitattributes
git commit -a -m ...
git branch test
git checkout test
echo "test" >> setup
git commit -a -m ...
git checkout master
echo "more content" >> setup
git commit -a -m ...
git merge test
Having said that I wonder if it's sensible to have setup in the repository at all. If you really want it under version control you could use submodules or the subtree merge strategy to keep common files in sync.
话虽如此,我想知道在存储库中进行设置是否明智。如果您真的希望它处于版本控制之下,您可以使用子模块或子树合并策略来保持公共文件同步。
回答by Adam Dymitruk
Have a smudge clean script in stead of separate branches. The script can be different depending on what machine you are on.
有一个涂抹干净的脚本而不是单独的分支。根据您使用的机器,脚本可能会有所不同。