How to resolve merge conflicts in vim
Introduction
vim
is definitely the best (IMHO) editor for editing any kind of text on the computer and git
for maintaining versions. While working with git
it is quite often the case that conflicts happen while merging
or rebasing
branches, but unfortunately I did not get any convenient way to resolve the conflicts. In situations like these, I used to resort to vscode
which had a great intuitive way of doing the same. Here, I'm glad to say that I've finally achieved vscode
level of intuitiveness and comfort, in vim <3. Follow along to know-how.
Goal
Resolve the conflict arising out of merging two branches of git
in vim.
Steps
Once you have merged the branches, you can do :Gstatus
inside vim to see the status of the files.
- Hit enter on the file whose conflicts you want to resolve.
- The above step will open the file in a new buffer.
- Go to the buffer in which the conflicted file is open then do
:Gvdiffsplit
to open the file in three-way split as shown below.
The left split is called the target
branch, it is the current state of the file when we tried merging i.e current changes. The middle one is the working
copy, where the resolved final state is captured. The rightmost split called the merge
branch, it has the changes from the branch being merged to the branch we are on i.e the incoming changes.
4. You can move to the next conflict using]c
and the previous conflict using [c
.
5. On each conflict you need to decide which changes you want, either you can accept changes from the target
branch by doing :diffget //2
or you can accept changes from merge
branch by doing :diffget //3
, all from the working
split. You can also you :diffput
but I prefer :diffget
.
6. If the syntax highlighting does not change even after resolving a conflict, you can use:diffupdate
to update the working copy. It is good to do :diffupdate
after resolving conflicts.
7. If you want both changes, the best option seems to be manually copying from target
and merge
splits.
8. You can also use:Gwrite
on either the target
or the merge
split you want that change entirely.
9. Once all conflicts are resolved, you can do:only
from the split you want to keep and leave the vimdiff
mode.
Conclusion
After having resolved the conflicts, you should do git merge --continue
after staging your changes by doing git add <files>
.
There are multiple ways you can go about resolving conflicts, let me you how you are doing it, in vim ;)
Catch me up on twitter
Originally published at http://souvikhaldar.info on September 28, 2020.