如果存在合并冲突,如何让git自动打开mergetool?

Mat*_*hen 25 git merge mergetool

如何让git自动运行git mergetool任何合并冲突?这应该适用于所有的合并,使用merge,rebase,pull,等.

oni*_*ake 7

你不能(还)让git这样做.


这可能是也可能不是可接受的解决方法.

在你的~/.bashrc:创建一个函数:

git() 
{ 
  if [[ $1 == "merge" ]] || [[ $1 == "rebase" ]] || [[ $1 == "pull" ]]; then 
    command git "$@" 
    rc=$?
    if [[ $rc == 1 ]]; then
      echo "There are conflicts, better run git-mergetool!!!"
      # There might be some other condition that returns a '1',
      # if so you can add another check like this:
      # if grep Conflicts $(git --git-dir)/MERGE_MSG;
      command git mergetool
    fi
  else 
    command git "$@"
  fi
}
Run Code Online (Sandbox Code Playgroud)

合并时不会调用Mergetool:

$ git merge non_conflicting_branch
Merge made by the 'recursive' strategy.
 bar | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 bar
Run Code Online (Sandbox Code Playgroud)

当存在冲突时调用Mergetool:

$ git merge conflicting_branch
Auto-merging foo
CONFLICT (content): Merge conflict in foo
Automatic merge failed; fix conflicts and then commit the result.
There are Conflicts, better run git-mergetool!!!
Run Code Online (Sandbox Code Playgroud)

没有在其他错误上调用Mergetool:

$ git merge adasds
fatal: adasds - not something we can merge
Run Code Online (Sandbox Code Playgroud)

  • @QuinnStrahl git 还不能做到这一点,那么通过 shell 完成它是一个错误的答案吗? (2认同)

j13*_*13r 4

您始终可以使用别名

\n\n
alias \'git-merge\'=\'git merge && git mergetool\'\nalias \'git-rebase\'=\'git rebase && git mergetool\'\nalias \'git-pull\'=\'git pull && git mergetool\'\n
Run Code Online (Sandbox Code Playgroud)\n\n

和/或按照这些思路编写帮助程序脚本

\n\n
#/bin/bash\ngit $*\n[ "$(git ls-files \xe2\x80\x93abbrev \xe2\x80\x93unmerged | wc -l)" -gt 0 ] && git mergetool\n
Run Code Online (Sandbox Code Playgroud)\n\n

进而

\n\n
alias git=\'~/.git/git-script\'\n
Run Code Online (Sandbox Code Playgroud)\n\n

没有直接调用 mergetool 的方法,因为它只是几种合并方法之一(请参阅 man 1 git-merge 中的“如何解决冲突”)。

\n

  • 正如我提到的,我只想在发生冲突时调用“mergetool”。“合并的几种方法中只有一种”的说法并不令人信服。是的,有多种方法,但选择一种作为默认方法是相当合理的。 (3认同)