【问题标题】:How to use VSCode 1.5+ as default editor for Git如何使用 VSCode 1.5+ 作为 Git 的默认编辑器
【发布时间】:2021-06-23 08:42:43
【问题描述】:

我正在尝试将 Git 配置为使用 VSCode 作为我的默认编辑器。我的问题是 VSCode 在打开文件后立即退出并将控制权交还给 PowerShell 提示 - 例如,在执行 rebase 时会导致问题。

我知道How to use Visual Studio Code as Default Editor for Git 并且我已经尝试了其中的所有技巧。我觉得那里的答案已经过时了,因为没有一个被接受的答案似乎产生了预期的结果。

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

PS C:\src\scp> code --wait

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

然后我尝试了以下所有方法,但它们都对我不起作用。

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

最后一次绝望的尝试

PS C:\src\scp> git config --global core.editor "'C:\Program Files (x86)\Microsoft VS Code\code.exe' -w"
PS C:\src\scp> 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:\src\scp> git --version
git version 2.31.1.windows.1

PowerShell

PS C:\src\scp> $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:\src\scp> git config --global core.editor "powershell -NoProfile -Command code -n --wait"
PS C:\src\scp> git rebase --continue
Aborting commit due to empty commit message.
error: could not commit staged changes.
PS C:\src\scp> git config --global core.editor "cmd /c code -n --wait"
PS C:\src\scp> git rebase --continue
Aborting commit due to empty commit message.
error: could not commit staged changes.

更新

使用 CMD

PS C:\src\scp> cmd
Microsoft Windows [Version 10.0.19042.1052]
(c) Microsoft Corporation. All rights reserved.

C:\src\scp>git config --global core.editor "code -w"

C:\src\scp>git rebase --continue
Aborting commit due to empty commit message.
error: could not commit staged changes.

更新

更新的 Git

PS C:\src\scp> 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 Files\Git'
  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 Files\Git\'

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:\ProgramData\chocolatey\logs\chocolatey.log).
PS C:\src\scp> git --version
git version 2.32.0.windows.1

【问题讨论】:

  • 它能在 git-bash 中工作吗?

标签: git powershell visual-studio-code


【解决方案1】:

你可以试试,如this issue

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

【讨论】:

  • 谢谢 :-) 这对我来说看起来很有希望,但不幸的是它给出了相同的结果。
  • @MarkCassidy 它是否适用于常规 git 提交(在变基之外)?
  • 没有。 PS C:\src\scp> git commit Aborting commit due to empty commit message.。我以前从未注意到它用于常规提交,因为我总是使用-m 提交。
  • @MarkCassidy 从 CMD 来看,这样会更好吗? (再次,为了测试而故意不带 -m 的提交)
  • 还是一样。我仍然怀疑 Git 本身,但我不知道如何。 git commit 也有相同的行为。
猜你喜欢
  • 2015-07-13
  • 2021-04-10
  • 2017-05-14
  • 1970-01-01
  • 2012-03-16
相关资源
最近更新 更多