今天我尝试在 Windows 命令提示符下使用 git mergetool
,发现它默认使用 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' 为此,请使用 Visual Studio Code 在 .gitconfig 中粘贴一些行。从命令行运行 git config --global core.editor "code --wait"。从这里您可以输入命令 git config -- global -e. 你需要在下面的“Extra Block”中粘贴代码。[user] name = EricDJohnson email = cool-email@neat.org [gui] recentrepo = E:/src/gitlab/App-Custom /Some-App # Comment: 你刚刚通过 'git config --global core.editor "code --wait"' 添加了这个 [core] editor = code --wait # Comment: Start of "Extra Block" # Comment: This是解锁 Visual Studio Code 作为你的 Git diff d Git 合并工具 [merge] tool = vscode [mergetool "vscode"] cmd = code --wait $MERGED [diff] tool = vscode [difftool "vscode"] cmd = code --wait --diff $LOCAL $REMOTE #评论:“额外块”结束
现在,在您的 Git 目录中运行有冲突的 git mergetool
,并且,tada,您有 Visual Studio Code 帮助您处理合并冲突! (只需确保在关闭 Visual Studio Code 之前保存您的文件。)
https://i.stack.imgur.com/SjhvD.png
有关从命令行启动 code
的更多信息,请查看 this documentation。
有关 git mergetool
中的更多信息,请查看 this documentation。
我不得不用简单的引号替换双引号:
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
使其正常工作(使用双引号, $LOCAL 和 $REMOTE 被它们的值替换)。
如果您对 Windows 使用 Git Bash 而不是 Windows 命令提示符,则需要这样做。
:^)
'
s)。
在 excellent existing answer 之上,您应该通过将 -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 代码编辑器窗口。它仍然默认为终端。我使用了错误的命令吗?
git diftool
而不是 git diff
?
使用手册,您可以找到一个有趣的论点:
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"
类似问题here
如果有人想在 Visual Studio 中解决它,另一个选项是通过 Visual Studio 进行:团队资源管理器 -> 单击主页图标 => 设置按钮 => 展开 Git 部分 => 单击全局设置
https://i.stack.imgur.com/8ZdKI.png
不定期副业成功案例分享
<<<< HEAD
和>>>> develop
?unknown tool: vscode
...我很确定从命令行调用 VsCode 你必须使用code
而不是vscode