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.
Problem
How to configure vim or nvim with git to get a comfortable mergetool experience?
Solution
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.
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)?
- In Vim normal mode place the cursor inside a right window in a merge conflict section (it is marked with a violet/pink highlight).
- Hit the keys dp (a shortcut from
:diffput
). - Code under the cursor will replace conflicted part in the MERGED file.
- Conflict solved, you are good to go.
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.
Hints
You may execute :Gw!
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.
Sources
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