3

I'm trying to configure Git to use VSCode as my default editor. My problem is that VSCode exits and gives control back to the PowerShell Prompt as soon as it opens a file - causing problems when, for example, doing a rebase.

I am aware of How to use Visual Studio Code as Default Editor for Git and I have tried every trick in there. I have a feeling the answer there is outdated since none of the accepted answers seem to produce the desired result.

First, to verify that VSCode does what I expect, I tried

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

And it works. VSCode opens and not until I close it am I returned to the command prompt.

Then I tried all of the below, and all of them fail to work for me.

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.

And the last desperate attempt

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.

So to me it seems like this problem isn't with VSCode at all, rather it's with the way Git calls the editor. Either that or I'm overlooking something else not-so-obvious.

Versions I use:

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

How can I get VSCode and Git to play nice together?

Update.

Tried a few more variations, as suggested in one answer. Same result.

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.

Update

Using 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.

Update

Updated 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
Mark Cassidy
  • 5,829
  • 1
  • 22
  • 33

2 Answers2

4

You can try instead, as seen in this issue:

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

See if that works then.

Note that a local configuration could overwrite that, so make sure to check the output of (from your local repository folder):

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

If there is a local setting, remove it with (from the same repository folder):

git config --unset core.editor
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks :-) It looked very promising to me, but unfortunately it gives the same result. – Mark Cassidy Jun 23 '21 at 09:22
  • @MarkCassidy Does it work for a regular git commit (outside of a rebase)? – VonC Jun 23 '21 at 09:28
  • No. `PS C:\src\scp> git commit Aborting commit due to empty commit message.`. I've never noticed it for regular commits before, since I always commit with `-m`. – Mark Cassidy Jun 23 '21 at 09:29
  • @MarkCassidy And from a CMD, would that work better? (again, with a commit without -m, on purpose, for testing) – VonC Jun 23 '21 at 09:38
  • still the same. I still suspect Git itself, but I don't see how. `git commit` also has same behaviour. – Mark Cassidy Jun 23 '21 at 09:40
  • @MarkCassidy Would git 2.32 change anything? (https://github.com/git-for-windows/git/releases) – VonC Jun 23 '21 at 09:42
  • Unfortunately no difference. – Mark Cassidy Jun 23 '21 at 10:14
  • 1
    @MarkCassidy What `git config --show-origin --show-scope core.editor` returns? – VonC Jun 23 '21 at 11:20
  • Omg I think you nailed it. `local file:.git/config code`. So at some point in some distant path perhaps I tried setting this and forgot the --global switch? that seems a plausible explanation – Mark Cassidy Jun 23 '21 at 11:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234113/discussion-between-vonc-and-mark-cassidy). – VonC Jun 23 '21 at 11:24
1

The 2017 issue 30562 "Git: Support editing the commit message in a text editor" just got updated for VSCode 1.69 (June 2022):

Today's Insiders release (2022-06-10) contains the changes to use the editor for the git commit input.

You can enable this capability using the git.useEditorAsCommitInput.

After enabling the setting you will have to restart VS Code so that the GIT_EDITOR environment variable is set.

The latest insider (2022-06-17) includes:

  • git.useEditorAsCommitInput is now enabled by default,
  • Added a new setting, git.terminalGitEditor, so that users can opt out when running git commit commands in the integrated terminal
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250