vimtricks.wiki Concise Vim tricks, one at a time.

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 ]c to jump through hunks and :diffget LOCAL or :diffget REMOTE for each
  • :diffget without arguments only works when there are exactly two diff buffers
  • After resolving all conflicts, :wqa writes all files and exits — git detects the merge is complete
  • Neovim users with vim.opt.diffopt can tune the diff algorithm (e.g., algorithm:histogram)

Next

How do I sort lines numerically so that 10 sorts after 2 rather than before it?