Efficient use of vimdiff to solve git merge conflicts (English version)

Source: Internet
Author: User
Tags git mergetool

If you are already a vim and git master and you are impatient, please jump to thequick start section at the end of this article.

1 set up vimdiff

The vimdiff as a merge tool will display several buffers to show the yours/theirs/original changes.

The key point is to set up the vimdiff so that the buffer number in the vimdiff status line is visible.

First, add following code into your ~ /. Vimrc

set laststatus=2 "show the status lineset statusline=%-10.3n  "buffer number

Second, if you know the buffer number, you can use hot key like ", 2" (Press comma first, then press two as quickly as possible) to pull change from buffer number two. add below code into your ~ /. Vimrc to set up hot keys:

map <silent> <leader>1 :diffget 1<CR> :diffupdate<CR>map <silent> <leader>2 :diffget 2<CR> :diffupdate<CR>map <silent> <leader>3 :diffget 3<CR> :diffupdate<CR>map <silent> <leader>4 :diffget 4<CR> :diffupdate<CR>
2 set up git

To use vimdiff as default merge tool:

git config --global merge.tool vimdiffgit config --global mergetool.prompt false
3 create a git project which has conflicting merges

I already set up a "hello World" project
Https://github.com/redguardtoo/test-git-mergetool for your practice.

It has three branches "master", "Bob", and "Chen ".

git clone git://github.com/redguardtoo/test-git-mergetool.gitcd test-git-mergetoolgit checkout -b bob origin/bob # create local mirror of bob branchgit checkout -b chen origin/chen # create local mirror of chen branch

Bob and Chen has edited same files. So please merge branch "Bob" into "master" at first. Then merge from "Chen". The merge conflicts will be created.

git branch # double check that we got three local branches: master, bob, chengit checkout master # set master branch as main branchgit merge bob #this is ok, because bob is the first one to merge changesgit merge chen # now some conflicts created because Bob has already edited and merged same files
4 resolve merge conflict

Now start merge tool:

git mergtool

Git will invoke vimdiff with the following window layout. There are four buffers in this layout:

Here is the explanation of each Buffer:

Buffer Explanation Buffer number
Theirs Contents of the file on the current Branch 2
Base Common base for the Merge 3
Yours Contents of the file to be merged. 4
Merged The file containing the conflict markers. You need edit and commit this file. 1

You cocould use hot key ", 2" (comma + two) ", 3" (comma + three) ", 4" (comma + Four) to pick the content you want to use (either theirs or base or yours) to replace the marked conflict in merged buffer whose buffer number is one.

Or you can edit the content directly in merged buffer. Anyway, git only care about the file binding to merged buffer. Any other buffer will be ignored by git.

You can use hot key "[C" and "] C" to navigate to previous/next conflict (including the conflict resolved by git automatically) in current file which is binding to merged buffer.

After finishing editing of the conflicting file in merged buffer, you can use hot key ": xa" to exit vimdiff. Git will open next conflicting file with vimdiff automatically.

When you have resolved all the conflicts, follow the hint of git to commit your changes.

5 tips
  • A Vim plugin called fugitive. Vim (https://github.com/tpope/vim-fugitive) Call do this too. I cannot write this article without reading its code.
  • You can use Emacs to do the similar job (http://stackoverflow.com/questions/1817370/using-ediff-as-git-mergetool). For me, emacs start up time is too much for this task.
    Some people use emacsclient which has other overhead which I don't like.
  • If you prefer merge tool with GUI, you can use command line 'git mergetool-T gvimdiff 'instead.
6
Quick Start

You can use command line like "Git mergetool-T vimdiff" to start vimdiff from git.

So the minimum set up is adding three lines of code into your ~ /. Vimrc:

map <silent> <leader>2 :diffget 2<CR> :diffupdate<CR>map <silent> <leader>3 :diffget 3<CR> :diffupdate<CR>map <silent> <leader>4 :diffget 4<CR> :diffupdate<CR>

", 2" ", 3" ", 4" to pull change from top three buffer. The bottom buffer is for editing the Code with markers which is actually your only work space.

": Help vimdiff" for other hot keys.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.