Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

solving merge conflict in vim

The vs-code has an amazing feature to solve merge conflicts as shown in the image..

image source google

We can easily select current,incoming or both the changes.. but in case of vim we have to manually do it which becomes problematic when file size is large...is there any nice way to do this in vim?

like image 660
Shreyansh Jain Avatar asked Aug 11 '18 01:08

Shreyansh Jain


2 Answers

There are a couple of things that are not clear in your question:

  1. How do you "select current, incoming or both"? Mouse click?
  2. What do you find problematic with Vim, exactly?

Anyway, here is how a basic merge conflict resolution looks after:

$ git mergetool --tool vimdiff

Three-way merge in Vim

  • The top-left window, called "LOCAL", shows the file as it is in the branch you are merging to. This is what's called "current" in your screenshot.
  • The top-right window, called "REMOTE", shows the file as it is in the branch you are merging from. This is what's called "incoming" in your screenshot.
  • The top-center window, called "BASE", shows the file as it was before any of the above changes. I can't see it in your screenshot.
  • The bottom window shows the file in its current state, with conflict markers. This is the equivalent of your current view. Except you don't have to edit conflict markers.

Usage is pretty simple:

  • to jump to next change (not "conflict"), press ]c, and [c for the previous change,
  • to choose LOCAL change, do :diffget LOCAL (or :diffg L for short),
  • to choose BASE change, do :diffg B,
  • to choose REMOTE change, do :diffg R,
  • to choose both change, remove the conflict markers, as there's no built-in command for that.

If you still find it too "manual" you can simply make your own mapping:

" choose LOCAL
nnoremap <Left> :diffget LOCAL<Bar>diffupdate<CR>
" choose REMOTE
nnoremap <Right> :diffget REMOTE<Bar>diffupdate<CR>
" choose BASE
nnoremap <Up> :diffget BASE<Bar>diffupdate<CR>
" choose both
nnoremap <Down> :/>>>>>>>/;?<<<<<<<?,.g/^\(<<<<<<<\\|=======\\|>>>>>>>\)/d<CR>
like image 127
romainl Avatar answered Nov 10 '22 02:11

romainl


My ConflictMotions plugin has motions to go to the next conflict [marker] (]x, ]=), corresponding text objects, and also a :ConflictTake command that lets you resolve the conflict by choosing some of the parts ([none this ours base theirs both all range query]).

like image 29
Ingo Karkat Avatar answered Nov 10 '22 04:11

Ingo Karkat