How do I selectively accept LOCAL or REMOTE changes in a three-way git merge using Vim?
Answer
:diffget LOCAL
Explanation
When Vim is configured as a git mergetool, it opens a three-way split with the LOCAL (your branch), REMOTE (their branch), and MERGED (the output file) buffers. Instead of manually editing conflict markers, you can use :diffget with a partial buffer name to pull changes from a specific side directly into the MERGED buffer.
How it works
:diffget LOCAL— pull the current hunk from the LOCAL (your) buffer into MERGED:diffget REMOTE— pull the current hunk from the REMOTE (their) buffer into MERGED:diffget BASE— pull from the BASE (common ancestor) buffer when using a 4-way merge (vimdiff4)
:diffget accepts any partial string that uniquely matches a buffer name — Vim resolves it to the correct buffer automatically. After accepting a hunk, run :diffupdate to refresh the diff highlighting.
Navigate between conflict hunks with:
]c— jump to next diff hunk[c— jump to previous diff hunk
Example
To configure Vim as your git mergetool:
git config merge.tool vimdiff3
Once in the three-way diff, with the cursor on a conflict hunk in the MERGED buffer:
<<<<<<< LOCAL
your version
=======
their version
>>>>>>> REMOTE
Running :diffget LOCAL replaces the hunk with the LOCAL version; :diffget REMOTE uses theirs.
Tips
- Use
]cto jump through hunks and:diffget LOCALor:diffget REMOTEfor each :diffgetwithout arguments only works when there are exactly two diff buffers- After resolving all conflicts,
:wqawrites all files and exits — git detects the merge is complete - Neovim users with
vim.opt.diffoptcan tune the diff algorithm (e.g.,algorithm:histogram)