如何解决Git中的合并冲突 [英] How to resolve merge conflicts in Git
问题描述
如何解决Git中的合并冲突?
How do I resolve merge conflicts in Git?
推荐答案
尝试:git mergetool
它将打开一个GUI,逐步引导您解决每个冲突,然后您可以选择合并方式.有时之后需要进行一些手动编辑,但通常仅此一项就足够了.这比肯定要手工完成整个事情要好得多.
It opens a GUI that steps you through each conflict, and you get to choose how to merge. Sometimes it requires a bit of hand editing afterwards, but usually it's enough by itself. It is much better than doing the whole thing by hand certainly.
根据@JoshGlover注释:
As per @JoshGlover comment:
命令
除非您安装一个GUI,否则不一定会打开一个GUI.对我来说运行
git mergetool
导致使用了vimdiff
.您可以安装 可以使用以下工具之一来代替它:meld
,opendiff
,kdiff3
,tkdiff
,xxdiff
,tortoisemerge
,gvimdiff
,diffuse
,ecmerge
,p4merge
,araxis
,vimdiff
,emerge
.
doesn't necessarily open a GUI unless you install one. Running
git mergetool
for me resulted invimdiff
being used. You can install one of the following tools to use it instead:meld
,opendiff
,kdiff3
,tkdiff
,xxdiff
,tortoisemerge
,gvimdiff
,diffuse
,ecmerge
,p4merge
,araxis
,vimdiff
,emerge
.
下面是使用vimdiff
解决合并冲突的示例过程.基于此链接
Below is the sample procedure to use vimdiff
for resolve merge conflicts. Based on this link
第1步:在终端上运行以下命令
Step 1: Run following commands in your terminal
git config merge.tool vimdiff
git config merge.conflictstyle diff3
git config mergetool.prompt false
这会将vimdiff设置为默认的合并工具.
This will set vimdiff as the default merge tool.
第2步:在终端中运行以下命令
Step 2: Run following command in terminal
git mergetool
第3步:您将看到以下格式的Vimdiff显示
Step 3: You will see a vimdiff display in following format
╔═══════╦══════╦════════╗
║ ║ ║ ║
║ LOCAL ║ BASE ║ REMOTE ║
║ ║ ║ ║
╠═══════╩══════╩════════╣
║ ║
║ MERGED ║
║ ║
╚═══════════════════════╝
这4个视图是
本地-这是当前分支中的文件
LOCAL – this is file from the current branch
BASE –共同祖先,两次更改之前文件的外观
BASE – common ancestor, how file looked before both changes
REMOTE –您要合并到分支中的文件
REMOTE – file you are merging into your branch
MERGED –合并结果,这就是保存在存储库中的内容
MERGED – merge result, this is what gets saved in the repo
您可以使用 ctrl + w 在这些视图之间导航.您可以先使用 ctrl + w ,然后按 j 进入MERGED视图.
You can navigate among these views using ctrl+w. You can directly reach MERGED view using ctrl+w followed by j.
有关vimdiff导航的更多信息此处和
More info about vimdiff navigation here and here
第4步.您可以通过以下方式修改MERGED视图
Step 4. You could edit the MERGED view the following way
如果您想从REMOTE获得更改
If you want to get changes from REMOTE
:diffg RE
如果您想从BASE获得更改
If you want to get changes from BASE
:diffg BA
如果您想从本地获得更改
If you want to get changes from LOCAL
:diffg LO
第5步.保存,退出,提交和清理
Step 5. Save, Exit, Commit and Clean up
:wqa
保存并退出vi
git commit -m "message"
git clean
删除差异工具创建的多余文件(例如* .orig).
git clean
Remove extra files (e.g. *.orig) created by diff tool.
这篇关于如何解决Git中的合并冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!