简体   繁体   English

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

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

Today I was trying to use the git mergetool on the Windows command prompt and realized that it was defaulting to use Vim , which is cool, but I'd prefer VS Code .今天我试图在 Windows 命令提示符下使用git mergetool并意识到它默认使用Vim ,这很酷,但我更喜欢VS Code

How can I have Visual Studio Code function as my GUI for handling merge conflicts (or even as a diffing tool) for Git?我如何将Visual Studio Code功能作为我的 GUI 来处理 Git 的合并冲突(甚至作为差异工具)?

As of Visual Studio Code 1.13 Better Merge was 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 .gitconfig and you have two options .将它们连接在一起的方法是修改您的.gitconfig并且您有两个选项

  1. To do this with command line entries, enter each of these: (Note: you may need to replace ' with " if you are not using Windows Git Bash, macOS or Linux as clarified by Iztok Delfin and e4rache)要使用命令行条目执行此操作,请输入以下各项:(注意:如果您不使用 Windows Git Bash、macOS 或 Linux,则可能需要将'替换为" ,如 Iztok Delfin 和 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 .gitconfig with Visual Studio Code .为此,请使用 Visual Studio Code.gitconfig粘贴一些行。

    • Run git config --global core.editor "code --wait" from the command line. git config --global core.editor "code --wait"运行git config --global core.editor "code --wait"

    • From here you can enter the command git config --global -e .从这里您可以输入命令git config --global -e You will want to paste in the code in the "Extra Block" below.您需要将代码粘贴到下面的“额外块”中。

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

Now from within your Git directory with a conflict run git mergetool and, tada, you have Visual Studio Code helping you handle the merge conflict!现在从有冲突的 Git 目录中运行git mergetool并且,tada,你有 Visual Studio Code 帮助你处理合并冲突! (Just make sure to save your file before closing Visual Studio Code.) (请确保在关闭 Visual Studio Code 之前保存您的文件。)

接受传入更改任何人?

For further reading on launching code from the command line, look in this documentation .有关从命令行启动code进一步阅读,请查看此文档

For more information in git mergetool check out this documentation .有关git mergetool更多信息,请查看此文档

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 Bash for Windows instead of Windows Command Prompt.如果您使用的是适用于 Windows 的Git Bash而不是 Windows 命令提示符,则需要这样做。

On top of the excellent existing answer , you should open VS Code in a new window by adding -n to the command line.除了现有优秀答案之外,您应该通过在命令行中添加-n来在新窗口中打开 VS Code。

So your git config --global --edit looks 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                                                    

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类似的问题在这里

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 部分 => 单击全局设置

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM