162

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

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

4

5 回答 5

310

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

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

  1. 要使用命令行条目执行此操作,请输入以下各项:(注意:如果您不使用 Iztok Delfin 和 e4rache 阐明的 Windows Git Bash、macOS 或 Linux,则可能需要替换'为)"

    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 with Visual Studio Code中粘贴一些行。

    • 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 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"
      

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

接受传入的更改任何人?

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

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

于 2017-06-14T16:07:39.897 回答
32

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

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

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

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

于 2018-08-19T10:09:05.357 回答
20

除了现有的优秀答案之外-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                                                    
于 2020-01-28T22:27:37.907 回答
8

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

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.

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

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

类似的问题在这里

于 2019-11-21T20:53:15.723 回答
2

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

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

于 2020-01-16T16:12:35.557 回答