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


当前回答

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

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

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

其他回答

试试:

git mergetool

它打开一个GUI,你通过每个冲突,你得到选择如何合并。有时它需要一些手编辑后,但通常它是足够的本身。


根据Josh Glover的评论:

[此命令] 不必打开 GUI 除非你安装一个. 运行 git mergetool 为我导致 vimdiff 被使用. 您可以安装下列工具之一来使用它: meld, opendiff, kdiff3, tkdiff, xxdiff, tortoisemerge, gvimdiff, diffuse, ecmerge, p4merge, araxis, vimdiff, emerge。


下面是使用vimdiff解决合并冲突的样本程序,基于此链接。

这个答案是为那些像我这样的Vim用户添加一个替代方案,他们更喜欢在编辑器内做任何事情。


饰 饰 DR

此分類上一篇


Tpope 帶來了這個偉大的插件為 Vim 稱為 fugitive. 一旦安裝,你可以執行 :Gstatus 檢查有衝突的檔案和 :Gdiff 開啟 Git 在三路合併。

一旦在三路合并,逃生会让你得到你合并的分支中的任何一个变化,如下:

:diffget //2,从原始(HEAD)分支中获取变更: :diffget //3,从合并分支中获取变更:

一旦你完成了合并文件,输入 :Gwrite 在合并的泡沫中。

Vimcasts发布了一个很好的视频,详细解释了这些步骤。

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

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

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

奖金:

在上面的回答中,谈论拖动 / 混合 / 混合,我想分享一个有趣而有成效的技巧,

git pull - 回归

上面的命令是我Git生活中最有用的命令,这节省了很多时间。

在推到远程服务器之前,尝试 git pull --rebase 而不是 git pull 和手动合并,它将自动同步最新的远程服务器变更(使用一个 fetch + 合并),并将您的本地最新的合并放在 Git 记录的顶部。

如果发生冲突,只需使用

git mergetool
git add conflict_file
git rebase --continue

查找详细信息: “git pull –rebase” 做什么?