I think many people, the moment a conflict occurs, are thrown out of the editor where they were comfortably reading diffs and forced to manually delete conflict markers while tracking them with their eyes in the terminal. Last time, I compared four git diff tools for Neovim: diffview.nvim, vim-fugitive, neogit, and gitsigns.nvim, and eventually settled on a combination of using diffview for reading diffs and leaving staging and branch operations to neogit. Looking back, I was only looking at diffs that could be reviewed peacefully, and I had never once tried the most painful scenario: merge conflicts. I was curious about what kind of screen diffview displays when a conflict occurs, so this time I will dig into just that.
As for the order of operations, I will first prepare a repository that actually causes a conflict, open diffview to check the screen, go through all the keymaps, and then actually resolve it and commit.
Preparing a repository that causes a conflict
Since I kept the repository I used last time, I created a new one for conflict verification.
/work ❯ mkdir demo-repo2
/work ❯ cd demo-repo2
/work/demo-repo2 ❯ git init -q
After creating an initial commit with a greet function in src/app.py that returns a greeting, I changed the wording of the greet function to something more polite in a branch called feature/polite-greeting, and changed the same greet function to different wording on the master side as well. Since both are rewriting the same line, it will definitely conflict when merged later.
/work/demo-repo2 ❯ git log --oneline --all --graph
* b498626 make greeting more polite | * 5943eed add extra excitement to greeting |/ * 6bd2412 initial commit
In this state, I will merge feature/polite-greeting into master.
/work/demo-repo2 ❯ git merge feature/polite-greeting
Auto-merging src/app.py CONFLICT (content): Merge conflict in src/app.py Automatic merge failed; fix conflicts and then commit the result.
/work/demo-repo2 ❯ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: src/app.py
no changes added to commit (use "git add" and/or "git commit -a")The conflict occurred successfully. I will open Neovim while in this state.
Opening DiffviewOpen during a conflict
Since the diffview.nvim plugin I installed last time is sufficient, this init.lua is kept simple.
~/.config/nvim/init.lua
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
vim.fn.system(
"git",
"clone",
"--filter=blob:none",
"https://github.com/folke/lazy.nvim.git",
"--branch=stable",
lazypath,
)
end
vim.opt.rtp:prepend(lazypath)
vim.g.mapleader = " "
vim.opt.number = true
vim.opt.termguicolors = true
require("lazy").setup(
"nvim-lua/plenary.nvim",
"sindrets/diffview.nvim",
config = function()
require("diffview").setup(
use_icons = false,
icons = folder_closed = "+", folder_open = "-" ,
signs = fold_closed = ">", fold_open = "v", done = "x" ,
)
end,
,
)/work/demo-repo2 ❯ nvim .
:DiffviewOpen
Instead of the usual three columns, it has changed to three columns with the unfamiliar headings OURS, LOCAL, and THEIRS. The official README states the following:
In my own words, if you open diffview during a merge or rebase, the conflicting files are grouped into a dedicated section, and it shows you a 3-way diff the moment you open them. The OURS on the left is the content on the master side, THEIRS on the right is the content on the feature side, and the LOCAL in the middle is the file in the actual working tree, with the conflict markers from <<<<<<< HEAD to >>>>>>> feature/polite-greeting displayed as they are. A new section called Conflicts(1) has been created in the file panel, and it is treated separately from the usual Changes or Staged changes.
Checking keymaps for conflicts with g?
Since I didn't know how to resolve it as it was, I first opened the help with g?.
g?
In the VIEW section, mappings specifically for conflict resolution were listed together.
leader co to adopt the OURS side of the conflict
leader ct to adopt the THEIRS side of the conflict
leader ca to keep both contents as they are
dx to delete the conflict itself
In the DIFF3 section below that, there were also two mappings: 2do and 3do. These are shorthand keys for diffget, which also exists in Vim's standard diff mode; 2do takes the hunk from the OURS side into LOCAL, and 3do takes the hunk from the THEIRS side into LOCAL. I understood that while the leader-based mappings select the entire conflict, 2do and 3do differ in that they allow for fine-grained selection on a per-hunk basis.
Resolving a single conflict
First, I will resolve the conflict in the greet function currently visible by using leader ct to choose the THEIRS version. I moved the cursor to the LOCAL window and pressed leader ct.
<leader>ct
The conflict markers in the LOCAL window disappeared cleanly, and the content was replaced with the same text as the THEIRS side. I will save the file.
:w
Upon saving, the remaining conflict count displayed to the right of app.py in the file panel changed to an x mark. This is thanks to setting signs done to x in init.lua, making it easy to identify files where all conflict markers have been removed. However, at this point, it is still unresolved in the git index, so I moved the cursor to the file panel. Looking at g? for the file panel, it mentioned that pressing s allows for staging or unstaging, so I pressed it.
s
The moment I pressed s, the file panel heading changed from Conflicts(1) to Staged changes(1), and the screen returned from the 3-column view of OURS, LOCAL, and THEIRS to the familiar 2-column diff view. The rest is the same as a normal merge; just commit from the terminal and it is done.
/work/demo-repo2 ❯ git commit -q -m "Merge feature/polite-greeting"
/work/demo-repo2 ❯ git log --oneline --all --graph
* 602b216 Merge feature/polite-greeting |\ | * b498626 make greeting more polite * | 5943eed add extra excitement to greeting |/ * 6bd2412 initial commit
The merge commit was successfully created. Being able to go straight from resolution to staging while staying in the 3-column screen was more comfortable than I imagined, as it eliminates the need to switch back and forth between the editor and terminal for every conflict.
Case with multiple conflicts in one file
In practice, there are often multiple conflicts in different parts of a single file, so I wanted to try that case as well. I created another repository and rewrote both the greet function and the add function separately on the master and feature sides.
/work ❯ mkdir demo-repo3
/work ❯ cd demo-repo3
/work/demo-repo3 ❯ git init -q
At first, I tried it with a short file containing only the greet and add functions, but when I rewrote both and merged them, the two changes were combined into one large conflict block. It seems that when git aggregates diffs, it treats them as a single hunk if the changes are close to each other. Therefore, I inserted functions that do nothing named spacer_one, spacer_two, and spacer_three in between to ensure the changes were sufficiently separated before recreating it.
/work/demo-repo3 ❯ git merge feature/two-spots
Auto-merging src/app.py CONFLICT (content): Merge conflict in src/app.py Automatic merge failed; fix conflicts and then commit the result.
/work/demo-repo3 ❯ cat src/app.py
def greet(name):
<<<<<<< HEAD
return f"Hello there, name!!"
=======
return f"Hi name, nice to see you"
>>>>>>> feature/two-spots
def spacer_one():
return 1
def spacer_two():
return 2
def spacer_three():
return 3
def add(a, b):
<<<<<<< HEAD
return a + b + 100
=======
return a + b + 0
>>>>>>> feature/two-spotsThis time, the greet function and add function remained as separate conflict markers. I opened diffview in this state.
/work/demo-repo3 ❯ nvim .
:DiffviewOpen
The right side of app.py in the file panel shows 2, and the status line also displays Conflict[1/2]. Diffview itself correctly counted and informed me that there were multiple conflicts in one file. Here, I will try pressing ]x, which I found in the g? help, to jump to the next conflict.
]x
Screen after pressing ]x. The cursor moved to the add function conflict, and the status line is now Conflict 2/2.
With the spacer function folded in between, the cursor jumped correctly to the add function conflict, and the status line changed to Conflict[2/2]. Here, I will use 2do to adopt the OURS hunk for the add function as is.
2do
Continuing, I returned to the first conflict with [x, and this time I will adopt the THEIRS hunk for the greet function.
[x
3do
The content of LOCAL became "Hi name, nice to see you" from the THEIRS side for the greet function, and "a + b + 100" from the OURS side for the add function, allowing me to choose the resolution source for each hunk within the same file. I realized that leader co and leader ct are suitable for major decisions at the file level, while 2do and 3do are better when you want to mix them, such as choosing OURS for one and THEIRS for another within the same file.
The rest follows the same flow as the single-conflict case: save, stage, and commit.
:w
s
/work/demo-repo3 ❯ git commit -q -m "Merge feature/two-spots"
/work/demo-repo3 ❯ git log --oneline --all --graph
* 15a4c0c Merge feature/two-spots |\ | * d6000e3 tweak greet and add on feature branch * | b97fc58 tweak greet and add on master |/ * 11ad138 initial commit
/work/demo-repo3 ❯ cat src/app.py
def greet(name):
return f"Hi name, nice to see you"
def spacer_one():
return 1
def spacer_two():
return 2
def spacer_three():
return 3
def add(a, b):
return a + b + 100A merge commit with the THEIRS content for greet and the OURS content for add was created exactly as intended.
What I learned from trying it out
In the previous article, I only looked at diffview as a tool for reading diffs, but it is actually designed to automatically switch to a dedicated 3-way diff when it detects that a conflict has occurred during a merge or rebase. This completely eliminates the need for tasks like running cat in the terminal and searching for markers by hand.
After actually using it, this is the order in which you should prioritize learning: first, when you want to decide the entire file at once, use leader co or leader ct; this will clear most conflicts. Only switch to 2do and 3do when you want to change the adoption source for each hunk within the same file, and use ]x and [x to jump around when they are scattered in multiple places. If you just remember how to use these three types, you can resolve conflicts without leaving the editor the next time one occurs.