如何让Git调用本地VS Code而非远程实例作为编辑器?
解决Git远程终端调用时本地打开VS Code编辑器的问题
环境配置
- 本地机器:Windows 11,VSCode版本1.85.1
- 远程机器:Windows 11虚拟机,VSCode版本1.85.0
- VSCode已设置为Git默认编辑器、差异工具及合并工具
- 通过VSCode集成终端的命令提示符执行Git命令
问题描述
执行需要打开编辑器的Git命令时,期望本地机器启动VS Code,但实际终端持续处于等待状态,必须按Ctrl+C中断操作。调试发现:
- 若远程机器(通过RDP连接GUI)已打开VS Code窗口,Git编辑器会在远程机器打开
- 若远程未打开VS Code窗口,不会启动新窗口,但任务管理器中会出现9个新的后台进程
需修改配置,让Git编辑器在本地机器打开。
VSCode集成终端输出
user@remote-machine C:\path\to\git\repo>git config --global --edit hint: Waiting for your editor to close the file...<用户输入Ctrl+C> error: There was a problem with the editor 'code --wait'.
本地机器全局.gitconfig文件
[core] editor = code --wait [diff] tool = vscode [difftool "vscode"] cmd = code --wait --reuse-window --diff $LOCAL $REMOTE [merge] tool = vscode [mergetool "vscode"] cmd = code --wait --merge $REMOTE $LOCAL $BASE $MERGED
本地机器相关扩展
- ms-vscode-remote.remote-ssh-edit
- ms-vscode.remote-explorer
- ms-vscode-remote.remote-ssh
远程机器全局.gitconfig文件
[core] editor = code --wait autocrlf = false [diff] tool = vscode [difftool "vscode"] cmd = code --wait --reuse-window --diff $LOCAL $REMOTE [merge] tool = vscode [mergetool "vscode"] cmd = code --wait --merge $REMOTE $LOCAL $BASE $MERGED
远程机器相关扩展
- ms-vscode.remote-explorer
- ms-vscode-remote.remote-ssh-edit
- ms-vscode-remote.remote-ssh
解决方案
问题核心是:通过VSCode远程终端执行Git命令时,默认调用的是远程机器上的code命令,而非本地程序。要让Git调用本地VSCode,需修改远程Git配置,指定本地VSCode的可访问路径。
步骤1:修改远程Git编辑器配置
在远程终端执行以下命令(替换为你本地VSCode的实际安装路径):
git config --global core.editor "'\\localhost\c$\Program Files\Microsoft VS Code\Code.exe' --wait"
若VSCode安装在用户目录下,路径示例:
git config --global core.editor "'\\localhost\c$\Users\你的用户名\AppData\Local\Programs\Microsoft VS Code\Code.exe' --wait"
步骤2:更新差异工具与合并工具配置
同样在远程终端执行命令,替换difftool和mergetool的调用路径:
# 更新差异工具配置 git config --global difftool.vscode.cmd "'\\localhost\c$\Program Files\Microsoft VS Code\Code.exe' --wait --reuse-window --diff $LOCAL $REMOTE" # 更新合并工具配置 git config --global mergetool.vscode.cmd "'\\localhost\c$\Program Files\Microsoft VS Code\Code.exe' --wait --merge $REMOTE $LOCAL $BASE $MERGED"
步骤3:验证配置
执行git config --global --edit,此时应弹出本地VSCode窗口,编辑保存关闭后,终端会自动恢复执行,不再卡住。
替代方案(SSH连接场景)
若使用SSH连接远程机器,可通过SSH转发调用本地code命令:
git config --global core.editor "ssh localhost 'code --wait \$1'"
内容的提问来源于stack exchange,提问作者keyur
相关产品推荐
相关产品推荐

