高效使用vimdiff解决git merge冲突(英文版)
来源:互联网 发布:java读取dat文件内容 编辑:程序博客网 时间:2024/05/21 01:12
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 at 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:
You could 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 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.
- 高效使用vimdiff解决git merge冲突(英文版)
- 高效使用vimdiff解决git merge冲突(英文版)
- 高效使用vimdiff解决git merge冲突(英文版)
- git解决冲突与merge
- 168 git merge冲突解决
- git Merge 冲突解决---流程
- 如何使用 vimdiff 来 git diff 和merge diff
- Git merge 冲突解决简明教程
- git merge合并分支解决冲突
- Git使用之——冲突解决一(git merge conflict)
- Git使用之——冲突解决一(git merge conflict)
- git权威指南总结七:git merge冲突解决
- eclipse使用git解决冲突
- git 使用P4Merge解决冲突
- Git 使用rebase解决冲突
- git merge/rebase冲突
- git merge出现冲突
- git merge冲突产生
- c3p0 和 dbcp的配置
- POJ2240 Arbitrage
- NYOJ214最长单调递增子序列(DP)
- POJ 2773 Happy 2006 【数论,容斥原理+二分】
- 选项菜单、子菜单、上下文菜单的使用
- 高效使用vimdiff解决git merge冲突(英文版)
- Linux中JDK1.6的安装和配置方法
- 一款移动电源的试用报告
- TW-k-means 算法初步了解
- 指针的应用2 15周 2
- C++ 获得本机IP
- Iperf 带宽性能测试使用方法与参数说明
- PHP+Mysql 实现留言板
- VMware下安装MS-DOS7.10