如何使用VSCode 1.5+作为Git的默认编辑器



我正在尝试将Git配置为使用VSCode作为我的默认编辑器。我的问题是,一旦PowerShell Prompt打开文件,VSCode就会退出并将控制权交还给它——例如,在执行重新基准时会导致问题。

我知道如何使用Visual Studio代码作为Git的默认编辑器,我已经尝试过其中的每一个技巧。我觉得那里的答案已经过时了,因为所有被接受的答案似乎都没有产生想要的结果。

首先,为了验证VSCode是否符合我的预期,我尝试了

PS C:srcscp> code --wait

它是有效的。VSCode打开,直到我关闭它,我才返回到命令提示符。

然后我尝试了下面的所有方法,但所有这些都无法为我工作。

PS C:srcscp> git config --global core.editor "code -w"
PS C:srcscp> git rebase --continue
Aborting commit due to empty commit message.
error: could not commit staged changes.
PS C:srcscp> git config --global core.editor "code --wait"
PS C:srcscp> git rebase --continue
Aborting commit due to empty commit message.
error: could not commit staged changes.
PS C:srcscp> git config --global core.editor "code --wait --new-window"
PS C:srcscp> git rebase --continue
Aborting commit due to empty commit message.
error: could not commit staged changes.

最后一次绝望的尝试

PS C:srcscp> git config --global core.editor "'C:Program Files (x86)Microsoft VS Codecode.exe' -w"
PS C:srcscp> git rebase --continue
Aborting commit due to empty commit message.
error: could not commit staged changes.

所以对我来说,这个问题似乎根本不是VSCode的问题,而是Git调用编辑器的方式的问题。要么是这样,要么我忽略了其他不那么明显的东西。

我使用的版本:

VSCode

Version: 1.56.2 (system setup)
Commit: redacted
Date: 2021-05-12T17:13:13.157Z
Electron: 12.0.4
Chrome: 89.0.4389.114
Node.js: 14.16.0
V8: 8.9.255.24-electron.0
OS: Windows_NT x64 10.0.19042

Git

PS C:srcscp> git --version
git version 2.31.1.windows.1

PowerShell

PS C:srcscp> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.19041.1023
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1023
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

我如何才能让VSCode和Git一起玩得很好?

更新。

正如一个答案中所建议的那样,尝试了更多的变体。同样的结果。

PS C:srcscp> git config --global core.editor "powershell -NoProfile -Command code -n --wait"
PS C:srcscp> git rebase --continue
Aborting commit due to empty commit message.
error: could not commit staged changes.
PS C:srcscp> git config --global core.editor "cmd /c code -n --wait"
PS C:srcscp> git rebase --continue
Aborting commit due to empty commit message.
error: could not commit staged changes.

更新

使用CMD

PS C:srcscp> cmd
Microsoft Windows [Version 10.0.19042.1052]
(c) Microsoft Corporation. All rights reserved.
C:srcscp>git config --global core.editor "code -w"
C:srcscp>git rebase --continue
Aborting commit due to empty commit message.
error: could not commit staged changes.

更新

更新的Git

PS C:srcscp> choco update git
Chocolatey v0.10.15
DEPRECATION NOTICE - choco update is deprecated and will be removed or
replaced in version 1.0.0 with something that performs the functions
of updating package indexes. Please use `choco upgrade` instead.
Upgrading the following packages:
git
By upgrading you accept licenses for the packages.
You have git v2.31.1 installed. Version 2.32.0 is available based on your source(s).
Progress: Downloading git.install 2.32.0... 100%
Progress: Downloading git 2.32.0... 100%
git.install v2.32.0 [Approved]
git.install package files upgrade completed. Performing other installation steps.
The package git.install wants to run 'chocolateyInstall.ps1'.
Note: If you don't run this script, the installation will fail.
Note: To confirm automatically next time, use '-y' or consider:
choco feature enable -n allowGlobalConfirmation
Do you want to run the script?([Y]es/[A]ll - yes to all/[N]o/[P]rint): a
Using Git LFS
Installing 64-bit git.install...
git.install has been installed.
git.install installed to 'C:Program FilesGit'
git.install can be automatically uninstalled.
Environment Vars (like PATH) have changed. Close/reopen your shell to
see the changes (or in powershell/cmd.exe just type `refreshenv`).
The upgrade of git.install was successful.
Software installed to 'C:Program FilesGit'
git v2.32.0 [Approved]
git package files upgrade completed. Performing other installation steps.
The upgrade of git was successful.
Software install location not explicitly set, could be in package or
default install location if installer.
Chocolatey upgraded 2/2 packages.
See the log for details (C:ProgramDatachocolateylogschocolatey.log).
PS C:srcscp> git --version
git version 2.32.0.windows.1

您可以尝试,如本期所示:

git config --global core.editor "cmd /c code -n --wait"
# or
git config --global core.editor "powershell -NoProfile -Command code -n --wait"

看看这样行不行。

请注意,本地配置可能会覆盖这一点,因此请确保检查(从本地存储库文件夹(的输出:

git config --show-origin --show-scope core.editor

如果有本地设置,请使用(从同一存储库文件夹中(删除:

git config --unset core.editor

2017年第30562"期;Git:支持在文本编辑器中编辑提交消息"刚刚更新了VSCode 1.69(2022年6月(:

今天的Insiders发布(2022-06-10(包含了对git提交输入使用编辑器的更改。

您可以使用git.useEditorAsCommitInput启用此功能。

启用设置后,您必须重新启动VS Code,以便设置GIT_EDITOR环境变量。

最新内幕消息(2022-06-17(包括:

  • git.useEditorAsCommitInput现在默认启用
  • 添加了一个新的设置git.terminalGitEditor,这样用户在集成终端中运行gitcommit命令时可以选择退出

最新更新