VS Code 可直接作为 Git 的 difftool 和 mergetool 使用,需先启用命令行工具 code,再配置 git config --global diff.tool vscode 与 mergetool vscode,并设置对应 cmd 命令及 --wait 参数。

VS Code 可以直接作为 Git 的 mergetool 和 difftool 使用,无需额外插件,只需正确配置即可。关键在于启用 VS Code 的命令行工具(code),并设置 Git 的对应选项。
这是前提。如果在终端中输入 code --version 报“command not found”,说明未启用 CLI 工具。
Cmd+Shift+P(macOS)或 Ctrl+Shift+P(Windows/Linux)调出命令面板Shell Command: Install 'code' command in PATH 并回车执行code --version 应能输出版本号用于查看文件差异(如 git diff 或 git difftool)。
git config --global diff.tool vscode<br>git config --global difftool.vscode.cmd 'code --wait --diff "$LOCAL" "$REMOTE"'
--wait:让 Git 等待你关闭比较窗口后再继续,避免终端卡住--diff:启动内建的左右对比模式"$LOCAL" "$REMOTE":Git 自动传入的临时文件路径(注意加引号防空格)用于解决合并冲突(如 git mergetool)。
git config --global merge.tool vscode<br>git config --global mergetool.vscode.cmd 'code --wait "$MERGED"'
"$MERGED" 是 Git 提供的冲突解决后保存的目标文件路径, <code>======, ),并提供“接受当前更改/入站更改/两者”等操作按钮
每次运行 git difftool 或 git mergetool 时,默认会询问是否启动工具。可跳过:
git config --global difftool.prompt falsegit config --global mergetool.prompt false基本上就这些。配置完成后,git difftool HEAD~1 file.js 会打开对比视图,git mergetool 会逐个加载冲突文件。不复杂但容易忽略 CLI 工具启用这一步。
以上就是将VS Code配置为Git的默认mergetool和difftool的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号