今天我尝试git mergetool
在 Windows 命令提示符下使用 并意识到它默认使用Vim,这很酷,但我更喜欢VS Code。
如何让Visual Studio Code功能作为我的 GUI 来处理 Git 的合并冲突(甚至作为差异工具)?
今天我尝试git mergetool
在 Windows 命令提示符下使用 并意识到它默认使用Vim,这很酷,但我更喜欢VS Code。
如何让Visual Studio Code功能作为我的 GUI 来处理 Git 的合并冲突(甚至作为差异工具)?
从Visual Studio Code 1.13开始, Better Merge被集成到 Visual Studio Code 的核心中。
将它们连接在一起的方法是修改你的.gitconfig
,你有两个选择。
要使用命令行条目执行此操作,请输入以下各项:(注意:如果您不使用 Iztok Delfin 和 e4rache 阐明的 Windows Git Bash、macOS 或 Linux,则可能需要替换'
为)"
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'
为此,请在.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
请查看此文档。
我不得不用简单的引号替换双引号:
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
使其正常工作(使用双引号, $LOCAL 和 $REMOTE 被它们的值替换)。
如果您使用的是 Windows 版Git Bash而不是 Windows 命令提示符,则需要这样做。
除了现有的优秀答案之外-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
使用手册,您可以找到一个有趣的论点:
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"
类似的问题在这里