How do I use vimdiff to resolve a conflict?

I just merged a branch into my master in git and I got Automatic merge failed; fix conflicts and then commit the result. Automatic merge failed; fix conflicts and then commit the result. Now I ran git mergetool and 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?

在这里输入图像描述


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).

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).

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; vim is 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.


@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.


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 to exit the Vim editor?). Git will then ask you if merge was complete, reply with n .


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

  • navigate to the bottom buffer (merge result): Ctrl-W j
  • navigate to next diff with j/k; or, better, use ] c and [ c to navigate to next and previous diff respectively
  • use zo while 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 version of code from local, remote or base version, or edit it and redo as you see fit
  • to get it from local version, use :diffget LO
  • from remote: :diffget RE
  • from base: :diffget BA
  • or, if you want to edit yourself, get a version from local/remote/base first, and then go to insert mode and edit the rest
  • once done, save the merge result, and quit all windows :wqa
  • normally, git detects that merge was done and completes the merge commit
  • You can search the Internet for other vimdiff shortcuts. I found this one useful for example: https://gist.github.com/hyamamoto/7783966

    链接地址: http://www.djcxy.com/p/22590.html

    上一篇: XCode:如何退出lldb swift repl

    下一篇: 我如何使用vimdiff解决冲突?