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


当前回答

我一直遵循下面的步骤,以避免冲突。

git checkout master (到主分支) git pull (更新你的主分支以获得最新的代码) git checkout -b mybranch (检查一个新的分支,并开始在该分支工作,以便你的主分支总是留在顶部) git add. and git commit and git push (在你的本地分支后你的变化) git checkout master (回到你的主分支)

现在你可以做同样的事情,并保持你想要的多家当地分支机构,并同时工作,只要在需要时对你的分支机构进行吉特支票。

其他回答

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

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

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

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

我要我的或他们的版本完整,或者想审查个别的变化,并决定每一个。

完全接受我的或他们的版本:

接受我的版本(本地,我们的):

git checkout --ours -- <filename>
git add <filename>              # Marks conflict as resolved
git commit -m "merged bla bla"  # An "empty" commit

接受其版本(远程,自己的):

git checkout --theirs -- <filename>
git add <filename>
git commit -m "merged bla bla"

如果你想做所有冲突文件运行:

git merge --strategy-option ours

git merge --strategy-option theirs

查看所有变更并单独接受它们

git mergetool 评论 改变并接受每个版本. git 添加 <filename> git commit -m "merged bla bla"

默认的 mergetool 在命令线上工作. 如何使用命令线 mergetool 应该是一个单独的问题。

您也可以为此安装视觉工具,例如通讯和运行

git mergetool -t meld

它将打开本地版本(ours),“base”或“合并”版本(合并的当前结果)和远程版本(天花板)。在完成时保存合并版本,运行 git mergetool -t 再次登记,直到您获得“不需要合并文件”,然后转到步骤3和4。

你可以以一些方式解决合并冲突,因为其他人已经详细说明了。

我认为真正的关键是知道如何在本地和远程存储库中流动的变化。 关键是理解跟踪分支. 我发现我认为跟踪分支是我之间的“中间失落的片段”我的本地,实际的文件目录和远程定义为起源。

而不是:

git add .
git commit -m"some msg"

相反,我做:

git add file,file2,file3...
git commit # Then type the files in the editor and save-quit.

因此,你更有意识到哪些文件被添加,你也可以查看列表,并在使用编辑器为消息时思考更多。

[更新 - 随着时间的流逝,我已经更换到:

git status # Make sure I know whats going on
git add .
git commit # Then use the editor

)

此外(更重要的是你的情况),我试图避免:

git pull

git pull origin master.

git checkout master
git fetch   
git rebase --hard origin/master # or whatever branch I want.

你也可能會發現這有用:

git 分支, fork, fetch, merge, rebase 和 clone,什么是差异?

对于想要半手动解决合并冲突的 Emacs 用户:

git diff --name-status --diff-filter=U

显示所有需要冲突解决的文件。

打开这些文件中的每个一个,或所有一次,通过:

emacs $(git diff --name-only --diff-filter=U)

当您访问需要在 Emacs 中编辑的泡沫时,

ALT+x vc-resolve-conflicts

这将打开三个泡沫(矿,他们的,和输出泡沫)。导航按“n”(下一个区域),“p”(预测区域)。按“a”和“b”复制矿或他们的区域到输出泡沫,分别。

当完成时:按“q”。Emacs问你是否想保存这个泡沫:是的。

git add FILENAME

当所有类型的泡沫结束时

git commit

结束合并。

使用耐心

对于一个大合并冲突,使用耐心为我提供了良好的结果,它会试图匹配区块而不是个别的线条。

例如,如果您更改您的程序的定位,默认 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>