如何使用Visual Studio代码作为Git MergeTool的默认编辑器

Eri*_*son 100 git cmd mergetool git-merge visual-studio-code

今天我试图使用git mergetoolWindows命令提示符,并意识到它默认使用VIM,这很酷,但我更喜欢VSCode.

如何将Visual Studio代码功能作为我的GUI来处理Git的合并冲突(甚至作为差异化工具)?

Eri*_*son 199

VSCode 1.13开始, Better Merge被整合到VSCode的核心中.

将它们连接在一起的方法是修改你的.gitconfig,你有两个选择.

  1. 要使用命令行输入为此,输入每个这些:(注:替换"'Windows上的Git的Bash由Iztok海豚和澄清e4rache)

    1. git config --global merge.tool vscode
    2. git config --global mergetool.vscode.cmd "code --wait $MERGED"
    3. git config --global diff.tool vscode
    4. git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
  2. 要通过在.gitconfig VS代码中粘贴一些行来完成此操作.

    • 运行git config --global core.editor "code --wait"命令行.
    • 从这里您可以输入命令git config --global -e.您需要粘贴下面"额外块"中的代码.

      [user]
          name = EricDJohnson
          email = cool-email@neat.org
      [gui]
          recentrepo = E:/src/gitlab/App-Custom/Some-App
      # Comment: You just added this via 'git config --global core.editor "code --wait"'
      [core]
          editor = code --wait
      # Comment: Start of "Extra Block"
      # Comment: This is to unlock VSCode as your git diff and git merge tool    
      [merge]
          tool = vscode
      [mergetool "vscode"]
          cmd = code --wait $MERGED
      [diff]
          tool = vscode
      [difftool "vscode"]
          cmd = code --wait --diff $LOCAL $REMOTE
      # Comment: End of "Extra Block"
      
      Run Code Online (Sandbox Code Playgroud)

现在来自你的git目录中的冲突运行git mergetool和tada,你有VSCode帮助你处理合并冲突!(只需确保在关闭VSCode之前保存文件).

接受传入改变任何人?

有关code从命令行启动的进一步阅读,请查看这些文档.

有关详细信息,git mergetool请查看这些文档.

  • 另外,这对我不起作用。它只是弹出打开的VsCode,并且从未弹出过文件 (9认同)
  • 在<<<< Head上方有一行,其中列出了选项:"接受当前更改|接受传入更改|接受两个更改|比较更改",我相信它会在文件中检测到的更改的每个部分插入此内容.但是在一个部分中,如果你想自定义合并一点这一点,我相信你会在你的本地进行更改,然后选择"接受当前更改"选项.因此,工作流程确实让您退后一步,以便向前迈进一步.如果其他人以不同的方式解决这个问题,请发布在这里教育我们 (3认同)
  • 我收到错误“未知工具:vscode” ...我很确定从命令行调用VsCode,您必须使用“ code”而不是“ vscode” (3认同)

小智 18

我不得不用简单的引号替换双引号:

  git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
Run Code Online (Sandbox Code Playgroud)

为了它正常工作.
(使用双引号,$ LOCAL和$ REMOTE将被其值替换)

如果您使用Git Bash for Windows而不是Windows命令提示符,则需要这样做.


mvd*_*mvd 17

除了现有优秀答案之外,您应该通过添加-n到命令行在新窗口中打开 VS Code 。

所以你git config --global --edit看起来像这样。

[merge]
        tool = vscode
[mergetool "vscode"]
        cmd = code -n --wait $MERGED
[diff]
        tool = vscode
[difftool "vscode"]
        cmd = code -n --wait --diff $LOCAL $REMOTE                                                    
Run Code Online (Sandbox Code Playgroud)


Gom*_*ino 6

使用手册你可以找到一个有趣的论点:

git difftool --help 
-x <command>, --extcmd=<command>
       Specify a custom command for viewing diffs.  git-difftool ignores the configured defaults and runs $command $LOCAL $REMOTE when this option is specified.
       Additionally, $BASE is set in the environment.
Run Code Online (Sandbox Code Playgroud)

有了这些信息,您可以轻松使用以下命令,而无需接触 git 配置:

git difftool -x "code --wait --diff" 
Run Code Online (Sandbox Code Playgroud)

类似的问题在这里