/ Vim

Using Vim or NeoVim as a git mergetool

The "ugly" 4 window view of the default vimdiff configuration can scare you out even if you were using Vim for a while. It's just too much information for most of the common merge cases.


How to configure vim or nvim with git to get a comfortable mergetool experience?

Not 4 windows...
4 windows vimdiff view


Install vim-fugitive and use following configuration in your ~/.gitconfig file:

[mergetool "nvim"]
  cmd = nvim -f -c \"Gdiff\" \"$MERGED\"

It will give you a "nice" view with 3 windows horizontally splitted.
vim 3 window vimdiff view
On the left side you have a current working copy (LOCAL).
On the right side you can view the remote (merged) file (REMOTE).
The central part of your screen shows a result (MERGED).

What is good about that setup (and why I use it all the time)?

  1. In Vim normal mode place the cursor inside a right window in a merge conflict section (it is marked with a violet/pink highlight).
    nvimdiff merging
  2. Hit the keys dp (a shortcut from :diffput).
  3. Code under the cursor will replace conflicted part in the MERGED file.
  4. Conflict solved, you are good to go.
    nvimdiff merged
    Repeat above steps for all merge conflicts (if you have more of them). You can navigate between left (LOCAL) and right (REMOTE) windows making diffputs to MERGED file as well as making manual changes directly inside merged content.


You may execute :Gwq command in any of the opened windows. The window where you execute that command will be commited and marked as a merge resolution. It is very useful if you want to use just REMOTE or just LOCAL version without any further merge resolution steps.

Want more?
When in the central window use d2o or d3o to pull changes from LOCAL or REMOTE file.


Where does it come from? Check out this tutorial if you would like to know the details vimcasts #33.
Also vim-fugitive docs are very helpful. Check out the https://github.com/tpope/vim-fugitive