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


当前回答

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

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

其他回答

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

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

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

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

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

而不是:

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,什么是差异?

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

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

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.

是的!

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