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


当前回答

查看 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

其他回答

如果你想从分支测试到大师合并,你可以遵循以下步骤:

步骤1:进入分支

git checkout test

步骤2:

git pull --rebase origin master

步骤3:如果有任何冲突,请转到这些文件来修改它。

步骤4:添加这些变化

git add #your_changes_files

步骤5:

git rebase --continue

步骤6:如果仍然存在冲突,再回到步骤3;如果没有冲突,就如下:

git push origin +test

步骤7:然后测试和主人之间没有冲突,你可以直接使用合并。

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

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

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.

是的!

我发现的最好的东西是使用“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>

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

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

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

GitLens for Visual Studio 代码

您可以尝试 GitLens for Visual Studio Code. 关键功能是:

3、轻松解决冲突

我喜欢这个特征:

此分類上一篇

第2章 现在的罪恶

此分類上一篇

第3章 错了

此分類上一篇

4、状态酒吧罪恶

此分類上一篇

有很多功能,你可以在这里查看。

当运行 git fetch 然后 git merge 当运行 git fetch 然后 git rebase 当运行 git pull (实际上相当于上述条件之一) 当运行 git stash pop 当应用 git patches (以电子邮件传输的文件出口的命令)

你需要安装一个合并工具,它是兼容的Git解决冲突. 我个人使用KDiff3,我发现它很好和方便. 你可以下载它的Windows版本在这里:

https://sourceforge.net/项目/kdiff3/文件/

然后设置 Git 配置以使用 KDiff3 作为其 mergetool:

$ git config --global --add merge.tool kdiff3
$ git config --global --add mergetool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
$ git config --global --add mergetool.kdiff3.trustExitCode false

$ git config --global --add diff.guitool kdiff3
$ git config --global --add difftool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
$ git config --global --add difftool.kdiff3.trustExitCode false

(请记住将路径替换为 KDiff3 EXE 文件的实际路径。

$ git mergetool

然后它打开Kdiff3,并首先试图自动解决合并冲突。

下面是Kdiff3看起来怎么样:

$ git mergetool
No files need merging