Last Updated: February 25, 2016
· mattboehm

Diff with common ancestor while resolving merge conflicts in vim (with fugitive)

If you use vim but don't use fugitive to resolve merge conflicts, I highly recommend watching http://vimcasts.org/episodes/fugitive-vim-resolving-merge-conflicts-with-vimdiff/ before reading the rest of this.

When resolving merge conflicts using fugitive.vim's :Gdiff, vim shows the version from the target branch, the version from the merging branch, and the working copy in a 3-way diff.

This shows how the branches differ from each other, but it's hard to see what changes have been made in each branch since they diverged from each other. I used to look at the other branch and try to guess how the file had changed based on my memory, but finally got tired of this and found an easier way.

When doing a 3-way diff, fugitive refers to the common ancestor of the two branches as :1. You can pass this to any of the fugitive commands that take a fugitive revision as an argument. Thanks to this feature, I've added the following to my .vimrc:

nnoremap <leader>ga :tab sp \| Gvedit :1 \| windo diffthis<CR>

When I'm in a 3-way diff and hit ,ga, vim opens a new tab and diffs the file in the active window against the common ancestor. When I'm done reading the diff, I just :tabclose and I'm right back to where I was.

Say Thanks
Awesome Job

16280de8 7c1b 11e7 8768 5c1fc377b8ec
Python Developer
Papendrecht, the Netherlands
Full Time