简体   繁体   English

git如何通过完全覆盖主分支将分支合并到主分支

[英]git How to merge a branch into the master branch by completely overwriting master branch

I started work on the master branch. 我开始在master分支上工作。 Almost one year ago, I created another branch dev in which I made some changes. 差不多一年前,我创建了另一个分支开发 ,我做了一些更改。 From that time on I continued work on dev branch. 从那时起,我继续在dev分支上工作。 Now I want to merge dev into master which results in lot of conflicts. 现在我想将dev合并到master中导致很多冲突。 I want to merge dev into master by overwriting master branch's contents ie for any conflict that arises I want to keep dev branch's version of the code. 我想通过覆盖master分支的内容将dev合并到master中,即出现任何冲突,我想保留dev分支的代码版本。 How can it be done ? 如何做呢 ?

You'll want to use a "merge using theirs" strategy, which is set using the -X flag 您将要使用“使用他们的合并”策略,该策略使用-X标志设置

git checkout master
git merge -X theirs dev

You can specify the strategy option with -X switch: 您可以使用-X开关指定策略选项

git checkout master
git merge -X theirs dev

A bit explanation. 有点解释。 The -X theirs means: Use recursive strategy with the merge but fallback to their changes if the strategy cannot resolve the conflict. -X theirs意思是:使用与合并递归策略,但退回到他们的变化,如果该策略无法解决冲突。

This is different from -s ours (for some reason there's no -s theirs ) which would be a greedy take ours always solution to conflicts. 这与-s ours (由于某种原因,没有 - -s theirs )不同,这将是我们总是解决冲突的贪婪。

git-merge(1) Merge Strategies section provides a deeper explanation on this. git-merge(1) Merge Strategies部分对此进行了更深入的解释。

您可以使用所有提交强制推送到master

git push -f origin master

As Paulo Bu noted , git provides different methods for this when you want a merge with an "ours" approach, but only one "out of the box" when you want a merge with a "theirs" approach. 正如Paulo Bu所说 ,当你想要一个“我们的”方法合并时,git为此提供了不同的方法,但是当你想要一个“他们的”方法合并时,只有一个“开箱即用”。 It's worth illustrating the two by example: 值得说明的是两个例子:

$ git merge -s ours deadbranch  # merge deadbranch and ignore all its code

or: 要么:

$ git merge -X ours livebranch  # merge livebranch but use ours when conflicting

Let me also note here that when you use git merge , git first finds the "merge base", the point at which the two branches were last in sync: 我还要注意,当你使用git merge ,git首先找到“merge base”,即两个分支最后同步的点:

            o - X   <-- master
          /
o - o - B
          \
            o - Y   <-- otherbranch

Here B represents the commit that is the merge-base, X is the tip of your branch ( master ), and Y is the tip of the other branch. 这里B表示作为合并库的提交, X是分支( master )的提示, Y是另一个分支的提示。 What git will do is diff B vs X and B vs Y . git会做的是diff B vs XB vs Y

Let's say that in "our" branch, master , we have files unchanged , no-conflicts , conflict , and removed . 让我们说在“我们的”分支中, master ,我们有文件unchangedno-conflictsconflictremoved These names are with respect to what happened in the other two branches. 这些名称与其他两个分支中发生的事情有关。

File unchanged is unchanged in the to-be-merged branch (that is, the diff from B to Y shows nothing at all for file unchanged ). 待合并分支中的文件未unchanged (即,从BY的差异对于文件未unchanged显示什么都unchanged )。 git merge never touches it, so nothing changes no matter what arguments we give to git merge . git merge永远不会触及它,所以无论我们为git merge提供什么参数,都没有任何变化。

The other files have some changes in master and/or the other branch. 其他文件在master和/或其他分支中有一些更改。

File no-conflicts has one change in master at the top, and one change in the other branch at the bottom. 文件no-conflicts在顶部的master中有一个更改,在底部的另一个分支中有一个更改。 These two changes do not conflict. 这两个变化不冲突。 git merge -s recursive will combine the changes, whether or not you use the -X ours option. git merge -s recursive将结合这些更改,无论你是否使用-X ours选项。 However, git merge -s ours will discard their change, keeping our version of file no-conflicts . 但是, git merge -s ours将放弃他们的更改,保持我们的文件版本no-conflicts For this case, the result is different. 对于这种情况,结果是不同的。

File conflict has one change in master at the top of the file, and a different change in the other branch also at the top. 文件conflict在文件顶部的master中有一个更改,另一个分支在顶部也有不同的更改。 (The changes could be anywhere, they just have to overlap. "At the top" makes it overlap.) That means these changes conflict. (变化可能在任何地方,它们只需要重叠。“在顶部”使它重叠。)这意味着这些变化发生冲突。 Using git merge -s recursive , you'll get a complaint and have to resolve the conflict. 使用git merge -s recursive ,您将收到投诉并且必须解决冲突。 Add -X ours and git will take your change, throwing away their change. 添加-X ours和git将采取你的改变,扔掉他们的变化。 Using git merge -s ours , git will take your change and throw away their change—so for this case, the result is the same. 使用git merge -s ours ,git会接受你的更改并丢弃他们的更改 - 所以对于这种情况,结果是一样的。

File removed has no change in master but is removed in the other branch. removed文件在master中没有变化,但在另一个分支中被删除。 In this case, git merge -s recursive will remove the file, whether or not you use -X ours . 在这种情况下, git merge -s recursive将删除文件,无论你是否使用-X ours However, git merge -s ours will ignore the removal. 但是, git merge -s ours将忽略删除。

In short, the difference between -s ours and -s recursive -X ours is that with the former, git completely ignores the diff from B to Y ; 简而言之, -s ours-s recursive -X ours的区别在于前者,git 完全忽略了从BY的差异 ; with the latter, git attempts to combine the diffs B -to- X (ours) and B -to- Y (theirs), but in the case of a conflict during combining, it picks the B -to- X change. 对于后者,git尝试将差异B to- X (我们的)和B to- Y (他们的)组合在一起,但是在组合期间发生冲突的情况下,它会选择BX变化。

With -X theirs (which is really -s recursive -X theirs ), git attempts to combine the diffs, and in the case of conflict, chooses the B -to- Y change over the B -to- X change. 使用-X theirs (这实际上是-s recursive -X theirs ),git尝试组合差异,并且在冲突的情况下,选择B to- Y更改B to- X更改。


It is possible to achieve the equivalent of -s theirs even though git does not have this built in. To do this, use git merge --no-commit otherbranch , then git rm -rf . 即使git没有内置它,也可以实现-s theirs的等效。为此,请使用git merge --no-commit otherbranch ,然后使用git rm -rf . from the top level to remove the resulting merge (with or without conflicts), then git checkout otherbranch -- . 从顶层删除生成的合并(有或没有冲突),然后git checkout otherbranch -- . from the top level to re-populate the tree and index based on otherbranch . 从顶层重新填充基于otherbranch的树和索引。 Then simply git commit the result. 然后只需git commit结果。 It's pretty rare to want this, though, which is why git does not have it as a built in strategy. 但是,很少有人想要这个,这就是为什么git没有将它作为内置策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM