Git 合并 -s 他们的:简单吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2303124/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:53:16  来源:igfitidea点击:

Git merge -s theirs: Simply?

gitmerge

提问by Dan Rosenstark

I have checked out the various questions on this. The first provides a huge question and answer(relevant? not sure) and the second provides a wrong answer as best answer.

我已经检查了有关此的各种问题。第一个提供了一个巨大的问题和答案(相关?不确定),第二个提供了错误的答案作为最佳答案。

I have a branch called great-use-this. I have another branch called master. I want to merge great-use-thisinto master, and avoid auto-merging conflicts.

我有一个名为great-use-this. 我有另一个分支叫master. 我想合并great-use-this到master,避免自动合并冲突。

What is the simplest and easiest way to do this?

执行此操作最简单和最简单的方法是什么?

Note:I have actually figured this out (using a third branch and ours, but this would be good to have on SO anyway.

注意:我实际上已经解决了这个问题(使用第三个分支 and ours,但是无论如何,这对 SO 来说会很好。

回答by VonC

Yes, creating a third branch and doing a merge -s oursis one solution.

是的,创建第三个分支并执行 amerge -s ours是一种解决方案。

But You will find the all "let's not advertised any "theirs" merging strategy" here.

但是你会在这里找到所有“让我们不要宣传任何“他们的”合并策略”

Between replacing your work with one other branch work, or simply getting rid of the current work and replacing it completelyby the other one, Junio C. Hamano (main Git Maintainer) prefers the second approach:

在用其他分支工作替换您的工作,或者干脆摆脱当前工作并完全替换为另一个工作之间,Junio C. Hamano(主要Git 维护者)更喜欢第二种方法:

I think "-s theirs" is even worse. It is how you would discard what you did (perhaps because the other side has much better solution than your hack), but that can be much more easily and cleanly done with:

$ git reset --hard origin/master

Some people might say "But with 'merge -s theirs', I can keep what I did, too". That reset is simply discarding what I did.

That logic also is flawed. You can instead:

$ git branch i-was-stupid 
$ git reset --hard origin/master

if you really want to keep record of your failure.

One big problem "-s theirs" has, compared to the above "reset to origin, discarding or setting aside the failed history" is that your 'master' history that your further development is based on will keep your failed crap in it forever if you did "-s theirs".

Hopefully you will become a better programmer over time, and you may eventually have something worth sharing with the world near the tip of your master branch. When that happens, however, you cannotoffer your master branch to be pulled by the upstream, as the wider world will not be interested in your earlier mistakes at all.

我认为“ -s theirs”更糟。这就是你如何放弃你所做的(也许是因为另一方比你的黑客有更好的解决方案),但这可以更容易、更干净地完成:

$ git reset --hard origin/master

有些人可能会说“但是有了' merge -s theirs',我也可以保留我所做的事情”。重置只是放弃我所做的。

这个逻辑也是有问题的。您可以改为:

$ git branch i-was-stupid 
$ git reset --hard origin/master

如果你真的想记录你的失败。

-s theirs与上述“重置原点,丢弃或搁置失败的历史”相比,“ ”的一个大问题是,如果您这样做了,那么您进一步开发所基于的“主”历史将永远保留您的失败废话“ -s 他们的”

希望随着时间的推移你会成为一个更好的程序员,并且你最终可能在你的 master 分支的尖端附近有一些值得与世界分享的东西。然而,当这种情况发生时,你不能让你的 master 分支被上游拉取,因为更广泛的世界根本不会对你之前的错误感兴趣。

回答by Sean Finney

Ran into this problem the other day:

前几天遇到了这个问题:

httpx://seanius.net/blog/2011/02/git-merge-s-theirs/

httpx://seanius.net/blog/2011/02/git-merge-s-theirs/

Update: Old url is down. Here is the article via Archive.org's Wayback Machine:

更新:旧网址已关闭。这是来自 Archive.org 的Wayback Machine的文章

git merge -s ours ref-to-be-merged
git diff --binary ref-to-be-merged | git apply -R --index
git commit -F .git/COMMIT_EDITMSG --amend

回答by Alan W. Smith

I'm leaning toward the git reset --hard BRANCHNAMEoption myself, but I've discovered that there is a "their" in Git (v. 1.7.1 at least).

git reset --hard BRANCHNAME自己倾向于这个选项,但我发现 Git 中有一个“他们的”(至少是 1.7.1 版)。

If you want to try it, just add an "-Xtheirs" argument to the merge command.

如果您想尝试一下,只需在合并命令中添加一个“-Xtheirs”参数即可。

For example, starting in master:

例如,从 master 开始:

git checkout -b editBranch
-- edit your files --
git add .
git commit -m "Updated the files"
git checkout master
git merge -Xtheirs editBranch

git checkout -b editBranch
-- edit your files --
git add .
git commit -m "Updated the files"
git checkout master
git merge -Xtheirs editBranch

If you have deleted any files in the editBranch, you'll get a merge conflit that can be resolved with git rm FILENAME.

如果您删除了 editBranch 中的任何文件,您将得到一个合并冲突,可以使用git rm FILENAME.

Once again, it seems likely that the reset --hard BRANCHNAME is better option, but if you have a case where you really need a theirs, this should get you there.

再一次,似乎 reset --hard BRANCHNAME 是更好的选择,但如果你有一个你真的需要他们的情况,这应该会让你到达那里。