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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44549733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:01:30  来源:igfitidea点击:

How to use Visual Studio Code as the default editor for Git MergeTool

gitvisual-studiovisual-studio-codegit-mergemergetool

提问by Eric D. Johnson

Today I was trying to use the git mergetoolon the Windows command prompt and realized that it was defaulting to use Vim, which is cool, but I'd prefer VS Code.

今天我试图git mergetool在 Windows 命令提示符下使用 ,并意识到它默认使用Vim,这很酷,但我更喜欢VS Code

How can I have Visual Studio Codefunction as my GUI for handling merge conflicts (or even as a diffing tool) for Git?

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

回答by Eric D. Johnson

As of Visual Studio Code 1.13Better Mergewas integrated into the core of Visual Studio Code.

Visual Studio Code 1.13 开始,Better Merge已集成到 Visual Studio Code 的核心中。

The way to wire them together is to modify your .gitconfigand you have two options.

将它们连接在一起的方法是修改您的.gitconfig和您有两个选项

  1. To do this with command line entries, enter each of these: (Note: replace "with 'on Windows Git Bash, macOS and Linux as clarified by Iztok Delfin and 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. To do this by pasting some line in the .gitconfigwith Visual Studio Code.

    • Run git config --global core.editor "code --wait"from the command line.
    • From here you can enter the command git config --global -e. You will want to paste in the code in the "Extra Block" below.

      [user]
          name = EricDJohnson
          email = [email protected]
      [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 Visual Studio Code 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"
      
  1. 要使用命令行输入为此,输入每个这些:(注:替换"'Windows上的Git猛砸,MacOS和Linux的通过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. 为此,请在.gitconfigwith Visual Studio Code 中粘贴一些行。

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

      [user]
          name = EricDJohnson
          email = [email protected]
      [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 Visual Studio Code 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"
      

Now from within your Git directory with a conflict run git mergetooland, tada, you have Visual Studio Code helping you handle the merge conflict! (Just make sure to save your filebefore closing Visual Studio Code.)

现在从您的 Git 目录中运行一个冲突,git mergetool并且,您有 Visual Studio Code 帮助您处理合并冲突!(请确保在关闭 Visual Studio Code 之前保存您的文件。)

Accept Incoming Change anyone?

接受传入更改任何人?

For further reading on launching codefrom the command line, look in this documentation.

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

For more information in git mergetoolcheck out this documentation.

有关更多信息,git mergetool请查看此文档

回答by e4rache

I had to replace the double quotes with simple quotes:

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

  git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'

for it to work properly (with double quotes, $LOCAL and $REMOTE are replaced by their values).

使其正常工作(使用双引号,$LOCAL 和 $REMOTE 被它们的值替换)。

This is needed if you are using Git Bashfor Windows instead of Windows Command Prompt.

如果您使用的是适用于 Windows 的Git Bash而不是 Windows 命令提示符,则需要这样做。

回答by Daniel B

In case if someone want to resolve it in Visual Studio, another option would be doing it through Visual Studio: Team Explorer -> click on Home icon => Settings button => expand Git section => click on Global Settings

如果有人想在 Visual Studio 中解决它,另一种选择是通过 Visual Studio:团队资源管理器 -> 单击主页图标 => 设置按钮 => 展开 Git 部分 => 单击全局设置

enter image description hereenter image description hereenter image description here

在此处输入图片说明在此处输入图片说明在此处输入图片说明

回答by mvd

On top of the excellent existing answer, you should open VS Code in a new window by adding -nto the command line.

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

So your git config --global --editlooks something like this.

所以你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                                                    

回答by Gomino

Using the manual you can find an interesting argument:

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

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.

With this information you can easily use the following command without touching the git configuration:

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

git difftool -x "code --wait --diff" 

Similar question here

类似的问题在这里