如何使用 Visual Studio 代码作为 git 的默认编辑器?

How to use Visual Studio Code as default editor for git?

在命令行中使用 git 时,我想知道是否可以使用 Visual Studio 代码作为默认编辑器,即在创建提交注释和查看文件差异时从命令行。

我知道不能用它来进行合并(至少现在是这样)但是有谁知道是否可以用它来查看差异,如果可以,什么命令行.gitconfig 文件中需要选项才能实现此目的吗?

更新 1:

我尝试了一种类似于我为 Notepad++ in the past 所做的方法,即

#!/bin/sh

"c:/Program Files (x86)/Notepad++/notepad++.exe" -multiInst -notabbar -nosession -noPlugin "$*"

并使用:

#!/bin/sh

"C:\Users\gep13\AppData\Local\Code\app-0.1.0\Code.exe" "$*"

但这会导致错误消息:

C:\temp\testrepo [master +1 ~0 -0]> git commit
[8660:0504/084217:ERROR:crash_reporter_win.cc(70)] Cannot initialize out-of-process crash handler
Aborting commit due to empty commit message.
C:\temp\testrepo [master +1 ~0 -0]>

代码正确打开,具有预期的内容,但它没有等待响应,即单击保存并关闭 window 到 return 以提示。

更新 2:

我刚刚收到一位开发 VSCode 的开发人员的回音。看起来目前不支持此功能:-(

https://twitter.com/IsidorN/status/595501573880553472

如果您有兴趣看到添加此功能,您可能需要考虑在此处投票:

http://visualstudio.uservoice.com/forums/293070-visual-studio-code/suggestions/7756482-support-git-configure-diff-and-merge-tools

更新 3:

我一直 reliably informed VSCode 团队已经选择了这个功能,所以我期待未来的版本包含它。

更新 4:

感谢@f-boucheros 下面的评论,我已经能够让 VS Code 作为提交评论、变基等的默认编辑器工作。我仍然想看看是否可以将它用作还有 diff 工具。

更新 5:

根据问题的公认答案,现在可以使用 V1.0 版本的代码。

我不确定您是否可以这样做,但是您可以在 gitconfig 文件中尝试添加这些内容。

尝试用这些值替换 kdiff3 以指向 visual studio 代码可执行文件。

[merge] tool = kdiff3 [mergetool "kdiff3"] path = C:/Program Files/KDiff3/kdiff3.exe keepBackup = false trustExitCode = false

我打开我的 .gitconfig 并修改为:

[core]
    editor = 'C:/Users/miqid/AppData/Local/Code/app-0.1.0/Code.exe'

这对我有用(我在 Windows 8)。

但是,我注意到在我尝试任意 git commit 之后,在我的 Git Bash 控制台中,我看到以下消息:

[9168:0504/160114:INFO:renderer_main.cc(212)] Renderer process started

不确定这可能会产生什么后果。

GitPad 将您当前的文本编辑器设置为 Git 的默认编辑器。

我在 Windows 10 中 .txt 文件的默认编辑器是 Visual Studio 代码和 运行 GitPad 曾经将其设为 [=] 的默认编辑器21=]。我没有遇到问题中提到的问题(Git 等到 VS Code window 在我的案例中关闭)。

.exe 文件的 link 对我不起作用,您可能需要自己编译源代码。)

我将 Visual Studio 代码设置为默认打开 .txt 文件。接下来我确实使用了简单的命令:git config --global core.editor "'C:\Users\UserName\AppData\Local\Code\app-0.7.10\Code.exe\'"。一切都很好。

据我了解,VSCode 已不在 AppData 中。

因此,通过在命令提示符中执行该命令来设置默认 git 编辑器 window:

git config --global core.editor "'C:\Program Files (x86)\Microsoft VS Code\code.exe' -w"

参数-w,--wait是等待window关闭后再返回。 Visual Studio 代码基于 Atom 编辑器。如果您还安装了 atom,请执行命令 atom --help。您会看到帮助中的最后一个参数是 wait。

下次你做 git rebase -i HEAD~3 它会弹出 Visual Studio 代码。一旦 VSCode 接近,那么 Git 将夺回领先优势。

注:我目前的VSCode版本是0.9.2

希望对你有所帮助。

好消息!在撰写本文时,此功能已在 0.10.12-insiders 版本中实现,并通过 0.10.14-insiders 执行。因此,我们将在即将推出的版本中使用它 1.0 Release of VS Code

实施参考:Implement -w/--wait command line arg

在最新版本(v1.0,于 2016 年 3 月 发布)中,您现在可以 use VS Code as the default git commit/diff tool。引用自文档:

  1. Make sure you can run code --help from the command line and you get help.

    • if you do not see help, please follow these steps:

      • Mac: Select Shell Command: Install 'Code' command in path from the Command Palette.

        • Command Palette is what pops up when you press shift + + P while inside VS Code. (shift + ctrl + P in Windows)
      • Windows: Make sure you selected Add to PATH during the installation.
      • Linux: Make sure you installed Code via our new .deb or .rpm packages.
  2. From the command line, run git config --global core.editor "code --wait"

Now you can run git config --global -e and use VS Code as editor for configuring Git. Add the following to enable support for using VS Code as diff tool:

[diff]
    tool = default-difftool
[difftool "default-difftool"]
    cmd = code --wait --diff $LOCAL $REMOTE

This leverages the new --diff option you can pass to VS Code to compare two files side by side.

To summarize, here are some examples of where you can use Git with VS Code:

  • git rebase HEAD~3 -i allows to interactive rebase using VS Code
  • git commit allows to use VS Code for the commit message
  • git add -p followed by e for interactive add
  • git difftool <commit>^ <commit> allows to use VS Code as diff editor for changes

运行 在您的 Mac 终端应用程序中执行此命令

git config --global core.editor "/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code"

在 windows 10 使用 64 位 Insiders 版本命令应该是:

git config --global core.editor "'C:\Program Files\Microsoft VS Code Insiders\bin\code-insiders.cmd'"

您也可以在'Program Files'目录中将'code-insiders.cmd'重命名为'code.cmd', 通过这种方式,您现在可以使用命令 'code .' 开始编辑 .目录

您需要使用命令:

git config --global core.editor "'C:\Program Files\Microsoft VS Code\code.exe' -n -w"

确保您可以从 Git Bash

启动编辑器

如果您想使用 Code.exe 短路径,您可以通过将以下行添加到您的 .bash_profile:

alias vscode="C:/Program\ Files/Microsoft\ VS\ Code/Code.exe"

现在,您可以仅使用 vscode 命令(或任何您命名的命令)来调用它

一些附加信息:

Setup will add Visual Studio Code to your %PATH%, so from the console you can type 'code' to open VS Code on that folder. You will need to restart your console after the installation for the change to the %PATH% environmental variable to take effect.

只想将这些反斜杠添加到以前的答案中,我在 Windows 10 CMD 上,如果空格前没有反斜杠,它就无法工作。

git config --global core.editor "C:\Users\your_user_name\AppData\Local\Programs\Microsoft\ VS\ Code\Code.exe"

另一个有用的选项是设置 EDITORVISUAL 环境变量。许多应用程序和实用程序使用这些环境变量来了解要使用的编辑器。如果未设置 core.editor,Git 也会使用其中之一(取决于 Git 版本)。

您可以使用以下方法为当前会话设置它:

export EDITOR="code --wait"
export VISUAL="$EDITOR"

不仅git这样,许多其他应用程序也会使用 VS Code 作为编辑器。

要使此更改永久生效,请将其添加到您的 ~/.profile 中。有关更多选项,请参阅 this question


这种方法的另一个优点是您可以为不同的情况设置不同的编辑器:

  1. 当您从本地终端工作时。
  2. 当您通过 SSH 会话连接时。

这对于 VS Code(或任何其他 GUI 编辑器)尤其有用,因为没有 GUI 它就无法工作。

在 Linux OS 上,将其放入您的 ~/.profile:

# Preferred editor for local and remote sessions
if [[ -n $SSH_CONNECTION ]]; then # SSH mode
  export EDITOR='vim'
else # Local terminal mode
  export EDITOR='code -w'
fi
export VISUAL="$EDITOR"

这样当你使用本地终端时,$SSH_CONNECTION环境变量将为空,所以会使用code -w编辑器,但是当你通过SSH连接时,$SSH_CONNECTION 环境变量将是一个 non-empty 字符串,因此将使用 vim 编辑器。它是控制台编辑器,因此即使您通过 SSH 连接也可以使用。


关于 EDITORVISUAL 环境变量的区别,请参阅 this question

除了 export EDITOR="code --wait",请注意,对于 VSCode v1.47(2020 年 6 月),这些差异编辑器将支持 VSCode reload/restart。
issue 99290:

with commit 1428d44, diff editors now have a chance to survive reloads and this works OK unless the diff editor on a git resource is opened as the active one:

commit 24f1b69 解决了这个问题)

git config --global core.editor "code --wait"

git config --global core.editor "code -w"

验证方式:

git config --global -e

您的配置将在 Visual Studio 代码中打开。

what command line options would be required in the .gitconfig file to make this happen?

我必须将此添加到我的 .git 配置文件中才能使 git-lens 工作:


[core]
    # Make sure that interactive rebases open correctly with vs code
    editor = code -w -n

-n 是可选的——正如亚历克斯在下面提到的 --new-window 的缩写,我发现它很有帮助:)

我在vscode terminal中添加了git bash作为默认值 如果你像我一样懒惰。那我劝你也跟我一样。

  • 第一步:打开设置:Ctrl + ,

  • 第二步:搜索.json --> 选择文件setting.json

  • 第 3 步:将代码粘贴到文件末尾的括号中 '}'

    "terminal.integrated.defaultProfile.windows": "gitbash", "terminal.integrated.shell.windows":"E:\app\git\Git\bin\bash.exe",

2022 年 Windows10 你应该有下一条路:

git config --global core.editor "'C:\Users\<your username here>\AppData\Local\Programs\Microsoft VS Code\code.exe' -w"