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

问题描述 投票:73回答:2

今天我试图在Windows命令提示符上使用git mergetool,并意识到默认使用VIM,这很酷,但我更喜欢VSCode。

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

git cmd visual-studio-code git-merge mergetool
2个回答
174
投票

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

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

  1. 要使用命令行条目执行此操作,请输入以下各项:(注意:在Iztok Delfin和e4rache澄清的Windows Git Bash上用"替换'git config --global merge.tool vscode git config --global mergetool.vscode.cmd "code --wait $MERGED" git config --global diff.tool vscode git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
  2. 要通过在.gitconfig with VS 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 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"

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

Accept Incoming Change anyone?

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

有关git mergetool的更多信息,请查看这些docs


12
投票

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.