我如何在我的 Git 存储库中解决合并冲突?
当前回答
对于使用 Visual Studio 的用户(Visual Studio 2015 在我的情况下)
关闭您的项目在Visual Studio. 特别是在大型项目,Visual Studio倾向于在使用UI合并时脱离。 立即在命令中进行合并. git checkout target_branch git merge source_branch 然后在Visual Studio中打开项目,然后转到Team Explorer → Branch。
其他回答
我发现的最好的东西是使用“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>
如果你有自动测试,运行这些,如果你有绳子,运行它,如果这是一个可构建的项目,然后在你承诺之前建造它,等等,在所有情况下,你需要做一些测试,以确保你的变化没有打破任何东西。
例如,如果你知道你和另一个人都在不同的重复工作,这两者都会影响相同的文件集,你应该提前彼此谈话,并更好地了解你每个人都在做什么类型的变化。
如果你不确定合并,不要强迫它。
这个答案是为那些像我这样的Vim用户添加一个替代方案,他们更喜欢在编辑器内做任何事情。
饰 饰 DR
此分類上一篇
Tpope 帶來了這個偉大的插件為 Vim 稱為 fugitive. 一旦安裝,你可以執行 :Gstatus 檢查有衝突的檔案和 :Gdiff 開啟 Git 在三路合併。
一旦在三路合并,逃生会让你得到你合并的分支中的任何一个变化,如下:
:diffget //2,从原始(HEAD)分支中获取变更: :diffget //3,从合并分支中获取变更:
一旦你完成了合并文件,输入 :Gwrite 在合并的泡沫中。
Vimcasts发布了一个很好的视频,详细解释了这些步骤。
请遵循以下步骤来解决 Git 中的合并冲突:
查看 Git 状态: git 状态 获取 patchset: git fetch (从您的 Git commit 查看正确的 patch) Checkout 一个本地分支(temp1 在我的例子中在这里): git checkout -b temp1 Pull 最近的内容从主: git pull -rebase 起源主 启动 mergetool 并检查冲突并纠正它们......并检查与您当前分支的远程分支中的变化: git mergetool Chec
如果您正在使用 IntelliJ IDEA 作为 IDE,请尝试通过:
git checkout <localbranch>
git merge origin/<remotebranch>
它将显示所有类似的冲突:
A_MBPro:test anu$ git merge 起源/ 自动合并 src/test/java/com/.../TestClass.java CONFLICT (内容): 合并冲突 src/test/java/com/.../TestClass.java
现在请注意,TestClass.java 文件在 IntelliJ IDEA 中显示为红色。
此外,Git 状态也将显示:
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: src/test/java/com/.../TestClass.java
在 IntelliJ IDEA 中打开文件,将有与
<<<<<<< HEAD
public void testMethod() {
}
=======
public void testMethod() { ...
}
>>>>>>> origin/<remotebranch>
在哪里,头部是你的本地分支和起源的变化/<remotebranch>是从远方分支的变化. 在这里保持你需要的东西,并删除你不需要的东西。
git add TestClass.java
git commit -m "commit message"
git push
我要我的或他们的版本完整,或者想审查个别的变化,并决定每一个。
完全接受我的或他们的版本:
接受我的版本(本地,我们的):
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添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别