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


当前回答

我跟随下面的过程。

解决合并冲突的过程:

首先,把最新的从目的地分支到你想合并 git pull 起源开发 随着你得到最新的从目的地,现在解决冲突手动在一个 IDE 通过删除这些额外的字符。

这就是它,你会看到它在你的拖请求中解决,如果你使用Bitbucket或GitHub。

其他回答

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

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

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

此分類上一篇

尝试编辑 Visual Studio 代码,如果您还没有。

一旦尝试合并(并在合并冲突中登陆),Visual Studio Code 会自动检测合并冲突。

它可以非常好地帮助你,通过显示原来的变更,以及你是否应该接受进入或

目前的变化(即合并前的原始变化)。

它帮助了我,它也可以为你工作!

PS:它只会工作,如果您与您的代码和Visual Studio代码配置了Git。

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

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

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 合并策略有时匹配单手 { 属于不同的功能。

git merge -s recursive -X patience other-branch

从文档中:

With this option, merge-recursive spends a little extra time to avoid 
mismerges that sometimes occur due to unimportant matching lines 
(e.g., braces from distinct functions). Use this when the branches to 
be merged have diverged wildly.

与共同祖先相比

如果你有一个合并冲突,想要看到其他人在修改分支时想知道什么,有时更容易将他们的分支直接与普通祖先(而不是我们的分支)进行比较。

git diff $(git merge-base <our-branch> <their-branch>) <their-branch>

通常,您只希望看到特定文件的更改:

git diff $(git merge-base <our-branch> <their-branch>) <their-branch> <file>

然而,答案是简单而令人沮丧的,合并冲突几乎总是可以手动解决的,如果你使用一个工具,如GitLab,GUI可能会帮助你在两个代码版本中找到差异,但在一天结束时,你必须决定哪条线应该保持,哪条线应该被删除。

一个简单的例子:程序员A和程序员B都将相同的 - 不同的修改 - 文件推到一个遥远的存储库.程序员A打开一个合并请求,GitLab强调了两个版本之间发生冲突的代码几条线。