我如何在我的 Git 存储库中解决合并冲突?


当前回答

从2016年12月12日起,您可以在github.com上合并分支和解决冲突。

因此,如果你不想使用命令线或任何第三方工具,从更古老的答案在这里提供,请使用GitHub的原住民工具。

这个博客帖子详细解释,但基本是,通过UI“融合”两个分支,你现在会看到一个“解决冲突”选项,将带你到一个编辑器,允许你处理这些融合冲突。

此分類上一篇

其他回答

下面是可能的使用案例,从顶部:

你会拖一些变化,但哦,你没有到目前为止:

git fetch origin
git pull origin master

From ssh://gitosis@example.com:22/projectname
 * branch            master     -> FETCH_HEAD
Updating a030c3a..ee25213
error: Entry 'filename.c' not uptodate. Cannot merge.

所以你更新并尝试一次,但有冲突:

git add filename.c
git commit -m "made some wild and crazy changes"
git pull origin master

From ssh://gitosis@example.com:22/projectname
 * branch            master     -> FETCH_HEAD
Auto-merging filename.c
CONFLICT (content): Merge conflict in filename.c
Automatic merge failed; fix conflicts and then commit the result.

所以你决定看看这些变化:

git mergetool

哦,我的,哦,我的,上流改变了一些事情,但只是用我的变化......不......他们的变化......

git checkout --ours filename.c
git checkout --theirs filename.c
git add filename.c
git commit -m "using theirs"

然后,我们试着最后一次。

git pull origin master

From ssh://gitosis@example.com:22/projectname
 * branch            master     -> FETCH_HEAD
Already up-to-date.

是的!

git fetch <br>
git checkout **your branch**<br>
git rebase master<br>

在此步骤中,您将尝试使用您最喜欢的 IDE 解决冲突。

您可以遵循此链接来查看如何在文件中解决冲突。

git add<br>
git rebase --continue<br>
git commit --amend<br>
git push origin HEAD:refs/drafts/master  (push like a drafts)<br>

现在一切都很好,你会发现你的承诺在吉里特。

查看 Stack Overflow 问题中的答案 堕胎在 Git 中的合并,特别是 Charles Bailey 的答案,显示如何查看不同版本的文件有问题,例如,

# Common base version of the file.
git show :1:some_file.cpp

# 'Ours' version of the file.
git show :2:some_file.cpp

# 'Theirs' version of the file.
git show :3:some_file.cpp

从2016年12月12日起,您可以在github.com上合并分支和解决冲突。

因此,如果你不想使用命令线或任何第三方工具,从更古老的答案在这里提供,请使用GitHub的原住民工具。

这个博客帖子详细解释,但基本是,通过UI“融合”两个分支,你现在会看到一个“解决冲突”选项,将带你到一个编辑器,允许你处理这些融合冲突。

此分類上一篇

我发现的最好的东西是使用“diff3”合并冲突风格:

<<<<<<<
Changes made on the branch that is being merged into. In most cases,
this is the branch that I have currently checked out (i.e. HEAD).
|||||||
The common ancestor version.
=======
Changes made on the branch that is being merged in. This is often a 
feature/topic branch.
>>>>>>>

如果冲突更长,那么我会把三个部分切成三个单独的文件,如“小”,“普通”和“天花板”。

diff common mine
diff common theirs

提示2

git log --merge -p <name of file>

如果你有自动测试,运行这些,如果你有绳子,运行它,如果这是一个可构建的项目,然后在你承诺之前建造它,等等,在所有情况下,你需要做一些测试,以确保你的变化没有打破任何东西。

例如,如果你知道你和另一个人都在不同的重复工作,这两者都会影响相同的文件集,你应该提前彼此谈话,并更好地了解你每个人都在做什么类型的变化。

如果你不确定合并,不要强迫它。