Why does git mergetool opens 4 windows in vimdiff? (I'd expect 3)

不羁岁月 提交于 2019-12-03 01:43:14

问题


When I met a conflict, I tried to use git-mergetool to solve it. I typed:

>git mergetool -t vimdiff

It opened vimdiff in 4-way, not 3-way. My split windows in vimdiff look like:

:ls
  1 #a   "Gemfile.lock"                 line 1
  2 %a   "Gemfile.lock.LOCAL.4828.lock" line 1
  3  a   "Gemfile.lock.BASE.4828.lock"  line 0
  4  a   "Gemfile.lock.REMOTE.4828.lock" line 0

What are they? I want a 3-way diff: target file, merge file and working file. How should I configure my git or vimdiff?


回答1:


As an alternative, have you thought about using fugitive?

I'm not going to lie to you; fugitive.vim may very well be the best Git wrapper of all time.

There is a an excellent vimcast, Fugitive.vim - resolving merge conflicts with vimdiff, by Drew Neil. This is part of a series on fugitive.

The Vimcasts website is a good place to learn more about vim.

To use fugitive as you mergetool you can use the following.

git config --global mergetool.fugitive.cmd 'vim -f -c "Gdiff" "$MERGED"'
git config --global merge.tool fugitive

Note: you may want to change vim to mvim or gvim.

Fugitive has a lot more to offer than just being a merge tool script so make sure you read the documentation and/or check out the vimcasts.




回答2:


After lots of research for issuing mergetool with vimdiff and only 3 windows, I came up with this configuration, that allows me to chose when I want 3 windows or the default 4 windows:

git config --global merge.tool vimdiff
git config --global alias.mt mergetool

git config --global mergetool.merge3.cmd 'vim -d -c "wincmd l" "$LOCAL" "$MERGED" "$REMOTE"'
git config --global alias.m3 'mergetool -t merge3'

Now you can start 3 windows just typing:

git m3

And the default (4 windows) will still works as expected with:

git mt

Also, you probably would like to add this lines to the end of your ~/.vimrc or /etc/vim/vimrc

 " shortcuts to vimdiff
 let mapleader=','
 let g:mapleader=','

 if &diff
    map <leader>1 :diffget LOCAL<CR>
    map <leader>2 :diffget BASE<CR>
    map <leader>3 :diffget REMOTE<CR>
 endif

This will create shortcuts like ,1 to grab from left, ,3 to grab from right (in both modes) and also ,2 to grab from base (center window) in the 4 windows mode.

That helps a lot!

My ~/.gitconfig file looks like this:

[user]
        name = Dr Beco
        email = my@email
[merge]
        tool = vimdiff
[mergetool "merge3"]
        cmd = vim -d -c \"wincmd l\" \"$LOCAL\" \"$MERGED\" \"$REMOTE\"
[alias]
        lo = log --pretty=format:\"%h %ce %cd %s\" --graph
        co = checkout
        ci = commit
        cm = commit -a -m
        st = status
        br = branch
        m3 = mergetool -t merge3
        mt = mergetool
[diff]
        tool = vimdiff

I hope this helps you (and those who get to here).




回答3:


Modifying a bit the commands from this page:

git config --global mergetool.vimdiff3.cmd 'vim -f -d "$LOCAL" "$MERGED" "$REMOTE"'
git config --global merge.tool vimdiff3
  • 'Merged' would be your working copy.
  • 'Local' the file that is in the branch you are trying to make the changes
  • 'Remote' the file from the branch you are trying to merge with.

And then you execute the command: git mergetool.

Note: I use fugitive also and highly recommend it.




回答4:


Note: while you can use only 3 Windows as described in Dr Beco's answer
(vim -d -c "wincmd l" "$LOCAL" "$MERGED" "$REMOTE"'),
the 4-windows mode is enhanced with git 2.8 (March 2016)

See commit 2300328 (29 Jan 2016) by Dickson Wong (diwo).
(Merged by Junio C Hamano -- gitster -- in commit 82c17b7, 17 Feb 2016)

The vimdiff backend for "git mergetool" has been tweaked to arrange and number buffers in the order that would match the expectation of majority of people who read left to right, then top down and assign buffers 1 2 3 4 "mentally" to local base remote merge windows based on that order.

Internally, git will now use:

"$merge_tool_path" -f -d -c '4wincmd w | wincmd J' \
            "$LOCAL" "$BASE" "$REMOTE" "$MERGED"

instead of:

"$merge_tool_path" -f -d -c 'wincmd J' \
        "$MERGED" "$LOCAL" "$BASE" "$REMOTE"

mergetool: reorder vim/gvim buffers in three-way diffs

When invoking default (g)vimdiff three-way merge, the merged file is loaded as the first buffer but moved to the bottom as the fourth window.
This causes a disconnect between vim commands that operate on window positions (e.g. CTRL-W_w) and those that operate on buffer index (e.g. do/dp).

This change reorders the buffers to have the same index as windows while keeping the cursor default to the merged result as the bottom window.




回答5:


I'll second the fugitive recommendation.

You could also try out splice.vim. It's a Vim plugin designed to act as a git or mercurial mergetool drop-in replacement. It allows you to easily shuffle various views of the conflict. It's also very quick, straightforward and does a good job at making merging more intuitive. Here's a screencast.

The files you've listed are:

  1. The local file containing the conflict.
  2. The file in the branch you're merging into.
  3. The file in the branch you're merging from.
  4. The file as it was in both branch ancesestor node. This file is very useful for figuring out what's going on!

Hope this helps.



来源:https://stackoverflow.com/questions/7309707/why-does-git-mergetool-opens-4-windows-in-vimdiff-id-expect-3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!