D   A   T   A   W   O   K





Creation: April 09 2016
Modified: September 11 2018

Git conflicts resolution with Vim

Vimdiff basics

:diffupdate - rescan the files for differences
]c - next difference
[c - previous difference
do - diff obtain (diffget)
dp - diff put (diffput)
zo - open folded text
zc - close folded text

Git built-in way

The command git mergetool is used to run one merge utility to resolve merge coflicts.

git mergetool [<file>...]

For each given parameter, the merge tool program will be run to resolve the conflicts on each file that has generated a conflict during the merge process. Specifying a directory will include all unresolved files in that path. In no file name is specified then it will automatically edit all the files with merge conflicts.

When we use vimdiff as a mergetool, the Vim windows are organized as following

    +--------------------------+
    | LOCAL  |  BASE  | REMOTE |
    +--------------------------+
    |          MERGED          |
    +--------------------------+

Where

Now, let's assume that you want to GET a change from the REMOTE version into the MERGED buffer. In the MERGED window, move the cursor to a conflict area and then type:

:diffget RE

This gets the corresponding change from REMOTE and puts it in the MERGED file. You can also type

:diffget BA     " to get from the BASE buffer
:diffget LO     " to get from the LOCAL buffer

There is also a command to PUT a change from a buffer to another. For example, if you want to PUT a change from the LOCAL buffer to the MERGED one; in the LOCAL window, move to a conflict area and then type

:diffput <MERGED-buffer-id or filename>

Fugitive Vim plugin

Fugitive Vim is a cool plugin that simplifies the Vim/Git integration for the most common operations.

A git conflict can be resolved by editing the file with vim and then start the diff tool with the command

:Gdiff      " Gvdiff for vertically split windows

The Vim windows are organized is this way

    +--------------------------+
    | LOCAL  | MERGED | REMOTE |
    +--------------------------+

Where the meaning of the names are pretty obvious at this point.

The conflicts are resolved in the same way as above, that is using the Vim built-in command diffget and gitput.

Once we have finished with the merge, we can use the Gwrite command to write the current file to the git index and exit from the vimdiff mode.

Fugitive-vim plugin can be downloaded here.

davxy