如何解决 Git 存储库中的合并冲突? [英] How to resolve merge conflicts in a Git repository?

查看:58
本文介绍了如何解决 Git 存储库中的合并冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解决我的 Git 存储库中的合并冲突.

我该怎么做?

解决方案

Try: git mergetool

它会打开一个 GUI,引导您完成每个冲突,您可以选择如何合并.有时它需要在之后进行一些手动编辑,但通常它本身就足够了.这肯定比手工完成要好得多.

根据 Josh Glover 的评论:

命令

<块引用>

除非您安装 GUI,否则不一定会打开 GUI.为我运行 git mergetool 导致使用了 vimdiff.你可以安装使用以下工具之一代替:meldopendiffkdiff3tkdiffxxdifftortoisemergegvimdiffdiffuse,ecmergep4mergearaxisvimdiffemerge.

以下是使用 vimdiff 解决合并冲突的示例过程.基于这个链接

步骤 1:在终端中运行以下命令

git config merge.tool vimdiffgit config merge.conflictstyle diff3git config mergetool.prompt 假

这会将 vimdiff 设置为默认合并工具.

第 2 步:在终端中运行以下命令

git 合并工具

第 3 步:您将看到以下格式的 vimdiff 显示

 ╔=======╦======╦========╗║ ║ ║ ║║ 本地 ║ 基地 ║ 远程 ║║ ║ ║ ║╠=======╩=====╩========╣║ ║║ 合并 ║║ ║╚=======================╝

这 4 个视图是

<块引用>

LOCAL – 这是来自当前分支的文件

<块引用>

BASE – 共同祖先,文件在两次更改之前的样子

<块引用>

REMOTE – 合并到分支中的文件

<块引用>

MERGED – 合并结果,这是保存在 repo 中的内容

您可以使用 ctrl+w 在这些视图之间导航.您可以使用 ctrl+w 后跟 j 直接进入合并视图.

关于 vimdiff 导航的更多信息在这里这里.

第 4 步.您可以通过以下方式编辑合并视图

如果您想从远程获取更改

:diffg RE

如果您想从 BASE 获取更改

:diffg BA

如果您想从 LOCAL 获取更改

:diffg LO

步骤 5.保存、退出、提交和清理

:wqa 保存退出vi

git commit -m "message"

git clean 删除由 diff 工具创建的额外文件(例如 *.orig).

I want to resolve merge conflicts in my Git repository.

How can I do that?

解决方案

Try: git mergetool

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.

As per Josh Glover's comment:

The command

doesn't necessarily open a GUI unless you install one. Running git mergetool for me resulted in vimdiff 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.

Below is the sample procedure to use vimdiff for resolve merge conflicts. Based on this link

Step 1: Run following commands in your terminal

git config merge.tool vimdiff
git config merge.conflictstyle diff3
git config mergetool.prompt false

This will set vimdiff as the default merge tool.

Step 2: Run following command in terminal

git mergetool

Step 3: You will see a vimdiff display in following format

  ╔═══════╦══════╦════════╗
  ║       ║      ║        ║
  ║ LOCAL ║ BASE ║ REMOTE ║
  ║       ║      ║        ║
  ╠═══════╩══════╩════════╣
  ║                       ║
  ║        MERGED         ║
  ║                       ║
  ╚═══════════════════════╝

These 4 views are

LOCAL – this is file from the current branch

BASE – common ancestor, how file looked before both changes

REMOTE – file you are merging into your branch

MERGED – merge result, this is what gets saved in the repo

You can navigate among these views using ctrl+w. You can directly reach MERGED view using ctrl+w followed by j.

More information about vimdiff navigation is here and here.

Step 4. You could edit the MERGED view the following way

If you want to get changes from REMOTE

:diffg RE

If you want to get changes from BASE

:diffg BA

If you want to get changes from LOCAL

:diffg LO

Step 5. Save, Exit, Commit and Clean up

:wqa save and exit from vi

git commit -m "message"

git clean Remove extra files (e.g. *.orig) created by diff tool.

这篇关于如何解决 Git 存储库中的合并冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆