Solving merge conflicts easily in Neovim with Fugitive
This article will not be a how to for fugitive. If you want to know how to use fugitive to its fullest I highly encourage you to take a look at documentation and help files for Fugitive. However, I will only touch on the bare minimum required to follow along with this article.
Merge conflicts need to be solved in a case by case basis. What I will show you is how I would solve it with fugitive. The example provided is to get you familiar with the relevant commands.
First we need to trigger the merge conflict, since this is about integration with fugitive let’s do it all in vim. Run :G merge feature
to trigger the merge conflict and run :Gvdiffsplit!
to open a three way split.
- The window on the left is the target branch in this case it is called “master”.
- The window in the middle is the working copy with familiar merge conflict prompts.
- The window on the right is the branch being merged into the target branch, master in this case. I named it “B”.
I want to include changes from both, so, I’ll need to see what my working copy would look like if I applied changes from both branches. Use the :diffget
command for this process. This is a very important command and it is simple to use.
:diffget
will apply the changes of only the selected branch onto the working copy. To apply the changes of only the master branch you need to know the name of the buffer. The target branch will have //2
in its name and the branch merging into the target branch will have //3
in its name.
Use tab complete for the :diffget
commad to select the appropriate buffer. Run :buffer
and a window will display the names of open buffers.
First let’s take a look at the changes applied from master.
# README
This is for the medium article. I'll put some random facts here :D
1. Vim fugitive is made by Tim Pope. A very popular and well known plugin author.
2. I've used Neovim for about two years but have only started to really get to know it this year
Now let’s inspect the changes from “B”. The buffer for branch “B” will have //3
in the same so run the same command with the appropriate buffer.
# README
While unrelated this line is for merge conflicts to keep things interesting.
1. I started programming in the early 2020 around when covid hit my country
as I had nothing better to do during quarantine.
2. This is from branch "B" :D
The next step is identifying changes.
- The sentence before the bullet points has changed.
- The bullet points have also changed.
The next logical step would be to find out what I want to keep.
- I don’t like either sentence, I’ll change it to “Here are some random facts.”
- I want all the facts from the bullet points minus the “This is from” line from both.
Now that you know what you want you need to apply to changes from either branch. In my case I used diffget
to apply the changes of “B”.
# README
While unrelated this line is for merge conflicts to keep things interesting.
1. I started programming in the early 2020 around when covid hit my country
as I had nothing better to do during quarantine.
2. This is from branch "B" :D
Now, I want the remaining two points. From “master” which means I’ll need to get to buffer which shows the contents of “master” and copy the two bullet points over, number them properly and remove the “this is from” line.
# README
While unrelated this line is for merge conflicts to keep things interesting.
1. Vim fugitive is made by Tim Pope. A very popular and well known plugin author.
2. I've used Neovim for about two years but have only started to really get to know it this year
3. I started programming in the early 2020 around when covid hit my country as I had nothing better to do during quarantine.
Happy with the changes, I save the file and commit the changes.