git mergetool
,发现它默认使用Vim,这很酷,但我更喜欢VS Code 。
如何将Visual Studio Code 设置为我的GUI以处理Git合并冲突(甚至作为差异工具)?
是否可以设置VS Code 以获取三方合并的可视化效果?
git mergetool
,发现它默认使用Vim,这很酷,但我更喜欢VS Code 。
如何将Visual Studio Code 设置为我的GUI以处理Git合并冲突(甚至作为差异工具)?
是否可以设置VS Code 以获取三方合并的可视化效果?
从 Visual Studio Code 1.13 开始,Better Merge 被整合到了 Visual Studio Code 的核心中。
将它们连接起来的方法是修改您的 .gitconfig
,您有两个选项。
通过命令行输入进行操作,输入以下每个命令:(注意:在 Windows 命令提示符下,请将 '
替换为 "
。感谢 Iztok Delfin 和 e4rache 帮助澄清此问题。)
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait --merge $REMOTE $LOCAL $BASE $MERGED'
git config --global diff.tool vscode
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
通过在.gitconfig
中粘贴一些命令来执行,可以使用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"]
# Comment: Original way before three-way merge shown commented out
# cmd = code --wait $MERGED
# Comment: For "Three-way merge"
cmd = code --wait --merge $REMOTE $LOCAL $BASE $MERGED
[diff]
tool = vscode
[difftool "vscode"]
cmd = code --wait --diff $LOCAL $REMOTE
# Comment: End of "Extra Block"
现在,在冲突的Git目录中运行git mergetool
,然后你会发现Visual Studio Code帮助你处理合并冲突!(只需确保在关闭Visual Studio Code之前保存文件。)
有关从命令行启动code
的更多信息,请查看此文档。
有关git mergetool
的更多信息,请查看此文档。
我必须将双引号替换为单引号:
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
为了使其正常工作(用双引号,$LOCAL和$REMOTE将被替换为它们的值)。
如果您使用Git Bash for Windows而不是Windows命令提示符,则需要这样做。
:^)
- Eric D. Johnson'
)。 - Eric D. Johnson除了现有的优秀答案之外,您还应该通过在命令行中添加-n
来在新窗口中打开VS Code。
因此,您的git config --global --edit
看起来像这样。
[merge]
tool = vscode
[mergetool "vscode"]
cmd = code --new-window --wait $MERGED
[diff]
tool = vscode
[difftool "vscode"]
cmd = code --new-window --wait --diff $LOCAL $REMOTE
git diff commit_id1 commit_id2
命令时,它不会弹出 VS code 编辑器窗口。它仍然默认为终端。我使用的命令错了吗? - user5965026git difftool
而不是 git diff
吗? - mvdcmd = code --wait --merge $REMOTE $LOCAL $BASE $MERGED
。 - Rafs使用说明书,您可以找到一个有趣的论点:
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"
这里有一个类似的问题,参见此处
<<<< HEAD
和>>>> develop
? - ghiscodingunknown tool: vscode
... 我相当确定如果要从命令行调用 VsCode,你需要使用code
而不是vscode
。请问需要我做什么翻译? - Kellen Stuart