如何使用 vimdiff 解决 git 合并冲突?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14904644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:31:50  来源:igfitidea点击:

How do I use vimdiff to resolve a git merge conflict?

gitvimgit-mergemergetoolgit-merge-conflict

提问by Cool Guy Yo

I just merged a branch into my master in git and I got Automatic merge failed; fix conflicts and then commit the result.Now I ran git mergetooland vimdiff opened with the image below. I don't know how to use vimdiff. What does each panel here mean and how should I proceed to fix the merge conflict?

我刚刚在 git 中将一个分支合并到我的 master 中,Automatic merge failed; fix conflicts and then commit the result.现在我跑了git mergetool,vimdiff 用下面的图片打开。我不知道如何使用 vimdiff。这里的每个面板是什么意思,我应该如何解决合并冲突?

enter image description here

在此处输入图片说明

采纳答案by chepner

All four buffers provide a different view of the same file. The top left buffer (LOCAL) is how the file looked in your target branch (what you are merging into). The top right buffer (REMOTE) is how the file looked in your source branch (where you are merging from). The middle buffer (BASE) is the common ancestor of the two (so you can compare how the left and right versions have diverged from each other).

所有四个缓冲区都提供同一文件的不同视图。左上角的缓冲区 (LOCAL) 是文件在您的目标分支中的外观(您正在合并的内容)。右上角的缓冲区 (REMOTE) 是文件在源分支(从哪里合并)中的外观。中间缓冲区 (BASE) 是两者的共同祖先(因此您可以比较左右版本彼此之间的差异)。

I may be mistaken on the following point. I think the source of the merge conflict is that both files have changed the same portion of the file since BASE; LOCAL has changed the quotes from double to single, and REMOTE has made the same change but also changed the background value from a color to a URL. (I think the merge is not smart enough to notice that all the changes to LOCAL are also present in REMOTE; it just knows that LOCAL has made changes since BASE in the same places that REMOTE has).

我可能在以下几点上有误。我认为合并冲突的根源是两个文件自 BASE 以来都更改了文件的同一部分;LOCAL 将引号从双引号更改为单引号,而 REMOTE 进行了相同的更改,但还将背景值从颜色更改为 URL。(我认为合并不够聪明,无法注意到对 LOCAL 的所有更改也出现在 REMOTE 中;它只知道 LOCAL 自 BASE 以来在 REMOTE 的相同位置进行了更改)。

In any case, the bottom buffer contains the file you can actually edit—the one sitting in your working directory. You can make any changes you like; vimis showing you how it differs from each of the top views, which are the areas that the automatic merge couldn't not handle. Pull changes from LOCAL if you don't want the REMOTE changes. Pull changes from REMOTE if you prefer those to the LOCAL changes. Pull from BASE if you think both REMOTE and LOCAL are wrong. Do something completely different if you have a better idea! In the end, the changes you make here are the ones that will actually be committed.

在任何情况下,底部缓冲区都包含您可以实际编辑的文件——位于您工作目录中的文件。您可以进行任何您喜欢的更改;vim正在向您展示它与每个顶视图的不同之处,这是自动合并无法处理的区域。如果您不想要 REMOTE 更改,请从 LOCAL 拉取更改。如果您更喜欢 LOCAL 更改,请从 REMOTE 拉取更改。如果您认为 REMOTE 和 LOCAL 都错误,请从 BASE 拉出。如果您有更好的主意,请做一些完全不同的事情!最后,您在此处所做的更改将是实际提交的更改。

回答by Timur

@chepner's answer is great, I would like to add some details on "how should I proceed to fix the merge conflict" part of the question. If you look into how to actually use vimdiff in this case, it goes below.

@chepner 的回答很好,我想在问题的“我应该如何着手解决合并冲突”部分添加一些细节。如果你研究在这种情况下如何实际使用 vimdiff,它会在下面。



First, to address the "abort everything" option - if you do not want to use "vimdiff" and want to abort the merge: press Esc, then type :qa!and hit Enter. (see also How do I exit the Vim editor?). Git will ask you if the merge was complete, reply with n.

首先,解决“中止一切”选项 - 如果您不想使用“vimdiff”并想中止合并:按Esc,然后键入:qa!并点击Enter。(另请参阅如何退出 Vim 编辑器?)。Git 会问你合并是否完成,回复n.



If you want to use vimdiff, here are some useful shortcuts. This assumes you know basics of Vim (navigation and insert/normal mode):

如果你想使用 vimdiff,这里有一些有用的快捷方式。这假设您了解 Vim 的基础知识(导航和插入/正常模式):

  • navigate to the bottom buffer (merge result): Ctrl-Wj
  • navigate to next diff with j/k; or, better, use ]cand [cto navigate to the next and previous diff respectively
  • use zowhile on a fold to open it, if you want to see more context
  • for each diff, as per @chepner's answer, you can either get the code from a local, remote or base version, or edit it and redo as you see fit
    • to get it from the local version, use :diffget LO
    • from remote: :diffget RE
    • from base: :diffget BA
    • or, if you want to edit code yourself, get a version from local/remote/base first, and then go to the insert mode and edit the rest
  • once done, save the merge result, and quit all windows :wqa
  • normally, git detects that the merge was made and creates the merge commit
  • 导航到底部缓冲区(合并结果): Ctrl-Wj
  • 使用j/导航到下一个差异k;或者,更好的是,使用]c[c分别导航到下一个和上一个差异
  • zo如果您想查看更多上下文,请在折叠时使用while 打开它
  • 对于每个差异,根据@chepner 的回答,您可以从本地、远程或基本版本获取代码,也可以根据需要进行编辑和重做
    • 要从本地版本获取它,请使用 :diffget LO
    • 从远程: :diffget RE
    • 从基地: :diffget BA
    • 或者,如果你想自己编辑代码,先从本地/远程/基地获取一个版本,然后进入插入模式并编辑其余的
  • 完成后,保存合并结果,并退出所有窗口 :wqa
  • 通常,git 检测到合并并创建合并提交

It does not appear to be possible to add both local and remote conflict hunks without copy pasting or custom shortcuts: https://vi.stackexchange.com/questions/10534/is-there-a-way-to-take-both-when-using-vim-as-merge-toolwhich is a shame since add add is such a common conflict type.

如果没有复制粘贴或自定义快捷方式,似乎无法同时添加本地和远程冲突块:https: //vi.stackexchange.com/questions/10534/is-there-a-way-to-take-both- when-using-vim-as-merge-tool这是一种耻辱,因为 add add 是一种常见的冲突类型。

To prevent vimdiff from asking you to press enter every time it starts, add to your .vimrc:

为了防止 vimdiff 在每次启动时都要求您按 Enter,请添加到您的.vimrc

set shortmess=Ot

as mentioned at: https://vi.stackexchange.com/questions/771/how-can-i-suppress-the-press-enter-prompt-when-opening-files-in-diff-mode

如在:https: //vi.stackexchange.com/questions/771/how-can-i-suppress-the-press-enter-prompt-when-opening-files-in-diff-mode 所述

You can search the Internet for other vimdiff shortcuts. I have found this one useful: https://gist.github.com/hyamamoto/7783966

您可以在 Internet 上搜索其他 vimdiff 快捷方式。我发现这个很有用:https: //gist.github.com/hyamamoto/7783966