【问题标题】:Multiple diff Tools多个差异工具
【发布时间】:2012-01-27 10:46:10
【问题描述】:

我已将我的 git 设置为使用 P4Merge 作为 diff 工具,如 here 所述。所以,git diff 将触发 P4Merge。

但是,有时我发现使用 UNIX diff 更快、更高效(因为不涉及 GUI)。如何设置 git 以便我可以轻松选择要触发的差异工具?这可能吗?例如:

$ git diff     # to trigger P4Merge
$ git difftool # to trigger UNIX diff

我的git config --list

code.editor=vi
merge.tool=p4merge
mergetool.extMerge.cmd=extMerge $BASE $LOCAL $REMOTE $MERGED
mergetool.extMerge.trustexitcode=false
diff.external=extDiff
mergetool.prompt=false
mergetool.keepbackup=false
mergetool.p4merge.path=/usr/local/bin/p4merge

【问题讨论】:

  • 上面提到的这里的网址坏了,能更新一下吗?

标签: git configuration


【解决方案1】:

您可以明确指出要使用的差异工具。我的~/.gitconfig 中有这个:

[diff]
    tool = vimdiff

[difftool "winmerge"]
    name = WinMerge
    trustExitCode = true
    cmd = "/c/Users/rkasten/Google\\ Drive/Apps/WinMerge/WinMergeU.exe" -u -e $LOCAL $REMOTE

然后我有以下别名设置:

alias gdt='git difftool'
alias gdtw='git difftool --tool=winmerge'

使用 masukomi 的示例,您只需将以下内容添加到您的 gitconfig:

[difftool "Kaleidoscope"]
    cmd = ksdiff --whatevs

并使用alias gdtk='git difftool --tool=Kaleidoscope' 来使用万花筒。

所有这些都可以在 git 2.7 中使用。

【讨论】:

    【解决方案2】:

    Git 可以轻松管理视觉和命令行差异。不需要花哨的调整

    git diff(默认)使用 unix diff

    如果您还想使用视觉差异工具,正确的设置方法是在您的 ~/.gitconfig 文件中包含如下部分:

    [difftool "Kaleidoscope"]
        cmd = ksdiff --partial-changeset --relative-path \"$MERGED\" -- \"$LOCAL\" \"$REMOTE\"
    

    cmd = 之后的内容特定于您的特定视觉差异应用程序。

    操纵git diff 将内容发送到视觉差异应用程序不是标准做法。因此,该问题寻求一个与内置功能 100% 倒退的答案。

    有关详细信息,请参阅git-difftool man page

    【讨论】:

      【解决方案3】:

      更新: 如果像 OP 你想让git difftool 调用默认的差异功能而不是外部工具,那么这个答案末尾的简单版本就可以了。但是,如果您只想要多个差异工具,我更喜欢和推荐的方法是 .gitconfig 文件方法,如DrStrangeprok's answer. 中所述


      您可以将以下 bash 脚本设置为您的外部编辑工具。
      (即:用它替换 extDiff 的内容)
      它不像 git diif1 / git diff2 那样简单,但它可以工作。

      编辑:更简单的版本见底部

      #!/bin/bash
      
      # Parameters from git:
      # 1    2        3       4        5        6       7
      # path old-file old-hex old-mode new-file new-hex new-mode
      
      CONTINUE=1
      while [ $CONTINUE == 1 ]
      do
      
          # Present Options
          echo "1. P4Merge"
          echo "2. Unix diff"
          echo "3. Cancel"
          echo -n "Choose diff tool[1]: "
      
          # Read in user choice
          read -n 1 OPTION
      
          # move down a line
          echo ""
      
          # if user didn't enter a choice default to 1
          if [[ $OPTION == "" ]] ; then
              OPTION="1"
          fi
      
          # Launch diff tool based on OPTION
          case $OPTION in
              1) /Applications/p4merge.app/Contents/MacOS/p4merge "$2" "$5" ; CONTINUE=0 ;;
              2) diff "$2" "$5" ; CONTINUE=0 ;;
              3) exit ;;
              *) echo "\"$OPTION\" is not valid " ;;
          esac
      
      
          # This sleep has two purposes
          # 1) on an invalid choice it delays the menu just a hair
          # 2) keeps this script alive long enough that git will not just move onto the next file
          #    instantly if diffing multiple files.
          sleep 1
      
      done
      

      没有菜单的简单版
      将此功能添加到您的 .bashrc 如果你使用 'git difftool' 那么它将使用默认的 diff

      function git {
      
          if [[ $1 == "difftool" ]] ; then
              git config --global --unset diff.external
              command git diff
              git config --global diff.external extDiff
          else
              command git "$@"
          fi
      
      }
      

      【讨论】:

      • 还有什么比这更简单的吗? :)
      • 是的,实际上,我只是想到了另一种更像你想要的方式,我在上面编辑了我的答案
      • @Appak 你可能需要改成command git "$@",否则git commit -am "all done" 将不起作用
      【解决方案4】:

      如果 Git 能够以某种方式为每个设置提供不同的设置,那就太好了,但如果不能,为了扩展 Appak 的答案,添加到 .bashrc 的以下内容可以调用文本差异、opendiff (FileMerge) 或 P4Merge 差异:

      function git {
          if [ $1 == "tdiff" ]
          then
              git config --global --unset diff.external
              command git diff
          elif [ $1 == "diff" ]
          then
              git config --global diff.external ~/bin/git-diff-opendiff
              command git diff
          elif [ $1 == "pdiff" ]
          then
              git config --global diff.external ~/bin/git-diff-p4diff
              command git diff
          else
              command git "$@"
          fi
      }
      

      只要确保您已经设置了正确的 git-diff-opendiffgit-diff-p4diff(它们是 shell 脚本)。

      【讨论】:

        【解决方案5】:

        你写

        但是,有时我发现使用 UNIX diff 更快,并且 更高效(因为不涉及 GUI)。如何设置 git 以便我可以轻松选择要触发的 diff 工具?

        所以这个问题的答案应该简单地利用 git 区分基于控制台和基于 gui 的差异工具的能力,而不是依赖于编写外部命令。

        通过向git difftool 提供-g 选项,要使用的差异工具将从配置的变量diff.guitool 而不是diff.tool 中读取。

        [alias]
            d = difftool -g
            dc = difftool
        [diff]
            guitool = p4merge
            tool = diff
        

        就是这样。您可以使用git d <file1> <file2> 或类似的方法调用您的gui 工具p4merge,并使用git dc <file1> <file2> 调用您的控制台工具。

        【讨论】:

          猜你喜欢
          • 2015-04-03
          • 1970-01-01
          • 1970-01-01
          • 2021-07-09
          • 2010-12-27
          • 1970-01-01
          • 1970-01-01
          • 2011-03-31
          • 1970-01-01
          相关资源
          最近更新 更多