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

时间:2022-04-23 00:22:35

When using git at the command line, I am wondering if it is possible to use Visual Studio Code as the default editor, i.e. when creating commit comments, and looking at a diff of a file from the command line.

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

I understand that it won't be possible to use it for doing merges (at least at the minute) but does anyone know if it is possible to use it for looking at diff's, and if so, what command line options would be required in the .gitconfig file to make this happen?

我明白用它做合并是不可能的(至少在一分钟之内)但是有没有人知道是否有可能用它来查看差异,如果有的话,需要什么命令行选项.gitconfig文件来实现这一目标?

UPDATE 1:

I have tried an approach similar to what I have done for Notepad++ in the past, i.e.

我尝试过类似于我过去为Notepad ++所做的方法,即

#!/bin/sh

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

And used:

#!/bin/sh

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

But this results in an error message:

但这会导致错误消息:

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]>

Code opens up correctly, with the expected content, but it isn't waiting on the response, i.e. clicking save and closing the window to return to prompt.

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

UPDATE 2:

I have just heard back from one of the developers working on VSCode. Looks like this functionality currently isn't supported :-(

我刚从一位致力于VSCode的开发人员那里听到了回复。看起来目前不支持此功能:-(

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

If you are interested in seeing this feature get added, you might want to think about adding your votes here:

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

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

UPDATE 3:

I have been reliably informed that this feature has been picked up by the VSCode team, so I am looking forward to a future release that will include it.

我已经可靠地获悉VSCode团队已经接受了这个功能,所以我期待将来的版本包括它。

UPDATE 4:

Thanks to @f-boucheros comment below, I have been able to get VS Code working as the default editor for commit comments, rebase, etc. I would still like to see if it is possible to use it as the diff tool as well.

感谢下面的@ f-boucheros评论,我已经能够让VS Code作为提交注释,rebase等的默认编辑器。我仍然希望看到是否可以将它用作diff工具。

UPDATE 5:

As per the accepted answer for the question, this is now possible using the V1.0 release of code.

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

11 个解决方案

#1


In the most recent release (v1.0, released in March 2016), you are now able to use VS Code as the default git commit/diff tool. Quoted from the documentations:

在最新版本(2016年3月发布的v1.0)中,您现在可以使用VS Code作为默认的git commit / diff工具。引文来自文件:

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

    确保您可以从命令行运行代码--help并获得帮助。

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

      如果您没有看到帮助,请按以下步骤操作:

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

        Mac:选择Shell命令:在命令选项板的路径中安装“代码”命令。

        • Command Palette is what pops up when you press shift + + P while inside VS Code. (shift + ctrl + P in Windows)
        • 命令选项板是在VS代码内按shift +⌘+ P时弹出的。 (Windows中的shift + ctrl + P)

      • Windows: Make sure you selected Add to PATH during the installation.
      • Windows:确保在安装期间选择了“添加到PATH”。

      • Linux: Make sure you installed Code via our new .deb or .rpm packages.
      • Linux:确保通过我们的新.deb或.rpm软件包安装了Code。

  2. From the command line, run git config --global core.editor "code --wait"
  3. 从命令行运行git config --global core.editor“code --wait”

Now you can run git config --global -e and use VS Code as editor for configuring Git. 如何使用Visual Studio代码作为Git的默认编辑器 Add the following to enable support for using VS Code as diff tool:

现在你可以运行git config --global -e并使用VS Code作为配置Git的编辑器。添加以下内容以支持将VS Code用作diff工具:

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

这利用了新的--diff选项,您可以将其传递给VS Code以并排比较两个文件。

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

总结一下,这里有一些例子,你可以在哪里使用Git和VS代码:

  • git rebase HEAD~3 -i allows to interactive rebase using VS Code
  • git rebase HEAD~3 -i允许使用VS Code进行交互式rebase

  • git commit allows to use VS Code for the commit message
  • git commit允许使用VS Code作为提交消息

  • git add -p followed by e for interactive add
  • git add -p后跟e进行交互式添加

  • git difftool <commit>^ <commit> allows to use VS Code as diff editor for changes
  • git difftool ^ 允许使用VS Code作为差异编辑器进行更改

#2


For what I understand, VSCode is not in AppData anymore.

据我所知,VSCode不再在AppData中了。

So Set the default git editor by executing that command in a command prompt window:

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

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

The parameter -w, --wait is to wait for window to be closed before returning. Visual Studio Code is base on Atom Editor. if you also have atom installed execute the command atom --help. You will see the last argument in the help is wait.

参数-w, - wait是在返回之前等待窗口关闭。 Visual Studio代码基于Atom编辑器。如果你还安装了原子,则执行命令atom --help。您将看到帮助中的最后一个参数是等待。

Next time you do a git rebase -i HEAD~3 it will popup Visual Studio Code. Once VSCode is close then Git will take back the lead.

下次你执行git rebase -i HEAD~3时会弹出Visual Studio Code。一旦VSCode关闭,Git将收回领先优势。

Note: My current version of VSCode is 0.9.2

注意:我当前的VSCode版本是0.9.2

I hope that help.

我希望有所帮助。

#3


You need to use command:

您需要使用命令:

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

Make sure you can start your editor from Git Bash

确保你可以从Git Bash开始编辑

If you want to use Code.exe with short path, you can do this by adding the following line to your .bash_profile:

如果要使用具有短路径的Code.exe,可以通过将以下行添加到.bash_profile来执行此操作:

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

And now, you can call it using only vscode command(or whatever you named it)

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

Some additional info:

一些额外的信息:

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.

安装程序会将Visual Studio代码添加到%PATH%,因此您可以在控制台中键入“code”以在该文件夹上打开VS Code。安装后需要重新启动控制台才能使更改为%PATH%环境变量生效。

#4


I opened up my .gitconfig and amended it with:

我打开了我的.gitconfig并修改了它:

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

That did it for me (I'm on Windows 8).

这样做对我来说(我在Windows 8上)。

However, I noticed that after I tried an arbitrary git commit that in my Git Bash console I see the following message:

但是,我注意到在我尝试了一个任意的git提交后,在我的Git Bash控制台中,我看到以下消息:

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

Unsure of what the ramifications of this might be.

不确定这可能是什么后果。

#5


GitPad sets your current text editor as the default editor for Git.

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

My default editor for .txt files in Windows 10 is Visual Studio Code and running GitPad once made it the default editor for Git. I haven't experienced the problems mentioned in the question (Git waits until VS Code window is closed in my case).

我在Windows 10中的.txt文件的默认编辑器是Visual Studio Code,并且运行GitPad曾经使它成为Git的默认编辑器。我没有遇到问题中提到的问题(在我的情况下,Git会等到VS Code窗口关闭)。

(The link for the .exe file didn't work for me, you may need to compile the source yourself.)

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

#6


Good news! At the time of writing, this feature has already been implemented in the 0.10.12-insiders release and carried out through 0.10.14-insiders. Hence we are going to have it in the upcoming version 1.0 Release of VS Code.

好消息!在撰写本文时,此功能已在0.10.12内部人员版本中实施,并通过0.10.14内部人员执行。因此,我们将在即将发布的VS Code 1.0版本中使用它。

Implementation Ref: Implement -w/--wait command line arg

实现参考:实现-w / - 等待命令行arg

#7


Run this command in your Mac Terminal app

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

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

#8


Just want to add these back slashes to previous answers, I am on Windows 10 CMD, and it doesn't work without back slashes before the spaces.

只想在前面的答案中添加这些反斜杠,我在Windows 10 CMD上,并且在空格之前没有反斜杠它不起作用。

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

#9


Im not sure you can do this, however you can try these additions in your gitconfig file.

我不确定你能做到这一点,但你可以在你的gitconfig文件中尝试这些添加。

Try to replace the kdiff3 from these values to point to visual studio code executable.

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

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

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

#10


I set up Visual Studio Code as a default to open .txt file. And next I did use simple command: git config --global core.editor "'C:\Users\UserName\AppData\Local\Code\app-0.7.10\Code.exe\'". And everything works pretty well.

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

#11


on windows 10 using the 64bit insiders edition the command should be:

在Windows 10上使用64位内部人员版本命令应该是:

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

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

you can also rename the 'code-insiders.cmd' to 'code.cmd' in the 'Program Files' directory, in this way you can now use the command 'code .' to start editing the files on the . directory

你也可以在'Program Files'目录中将'code-insiders.cmd'重命名为'code.cmd',这样你就可以使用命令'code'了。开始编辑文件。目录

#1


In the most recent release (v1.0, released in March 2016), you are now able to use VS Code as the default git commit/diff tool. Quoted from the documentations:

在最新版本(2016年3月发布的v1.0)中,您现在可以使用VS Code作为默认的git commit / diff工具。引文来自文件:

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

    确保您可以从命令行运行代码--help并获得帮助。

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

      如果您没有看到帮助,请按以下步骤操作:

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

        Mac:选择Shell命令:在命令选项板的路径中安装“代码”命令。

        • Command Palette is what pops up when you press shift + + P while inside VS Code. (shift + ctrl + P in Windows)
        • 命令选项板是在VS代码内按shift +⌘+ P时弹出的。 (Windows中的shift + ctrl + P)

      • Windows: Make sure you selected Add to PATH during the installation.
      • Windows:确保在安装期间选择了“添加到PATH”。

      • Linux: Make sure you installed Code via our new .deb or .rpm packages.
      • Linux:确保通过我们的新.deb或.rpm软件包安装了Code。

  2. From the command line, run git config --global core.editor "code --wait"
  3. 从命令行运行git config --global core.editor“code --wait”

Now you can run git config --global -e and use VS Code as editor for configuring Git. 如何使用Visual Studio代码作为Git的默认编辑器 Add the following to enable support for using VS Code as diff tool:

现在你可以运行git config --global -e并使用VS Code作为配置Git的编辑器。添加以下内容以支持将VS Code用作diff工具:

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

这利用了新的--diff选项,您可以将其传递给VS Code以并排比较两个文件。

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

总结一下,这里有一些例子,你可以在哪里使用Git和VS代码:

  • git rebase HEAD~3 -i allows to interactive rebase using VS Code
  • git rebase HEAD~3 -i允许使用VS Code进行交互式rebase

  • git commit allows to use VS Code for the commit message
  • git commit允许使用VS Code作为提交消息

  • git add -p followed by e for interactive add
  • git add -p后跟e进行交互式添加

  • git difftool <commit>^ <commit> allows to use VS Code as diff editor for changes
  • git difftool ^ 允许使用VS Code作为差异编辑器进行更改

#2


For what I understand, VSCode is not in AppData anymore.

据我所知,VSCode不再在AppData中了。

So Set the default git editor by executing that command in a command prompt window:

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

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

The parameter -w, --wait is to wait for window to be closed before returning. Visual Studio Code is base on Atom Editor. if you also have atom installed execute the command atom --help. You will see the last argument in the help is wait.

参数-w, - wait是在返回之前等待窗口关闭。 Visual Studio代码基于Atom编辑器。如果你还安装了原子,则执行命令atom --help。您将看到帮助中的最后一个参数是等待。

Next time you do a git rebase -i HEAD~3 it will popup Visual Studio Code. Once VSCode is close then Git will take back the lead.

下次你执行git rebase -i HEAD~3时会弹出Visual Studio Code。一旦VSCode关闭,Git将收回领先优势。

Note: My current version of VSCode is 0.9.2

注意:我当前的VSCode版本是0.9.2

I hope that help.

我希望有所帮助。

#3


You need to use command:

您需要使用命令:

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

Make sure you can start your editor from Git Bash

确保你可以从Git Bash开始编辑

If you want to use Code.exe with short path, you can do this by adding the following line to your .bash_profile:

如果要使用具有短路径的Code.exe,可以通过将以下行添加到.bash_profile来执行此操作:

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

And now, you can call it using only vscode command(or whatever you named it)

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

Some additional info:

一些额外的信息:

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.

安装程序会将Visual Studio代码添加到%PATH%,因此您可以在控制台中键入“code”以在该文件夹上打开VS Code。安装后需要重新启动控制台才能使更改为%PATH%环境变量生效。

#4


I opened up my .gitconfig and amended it with:

我打开了我的.gitconfig并修改了它:

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

That did it for me (I'm on Windows 8).

这样做对我来说(我在Windows 8上)。

However, I noticed that after I tried an arbitrary git commit that in my Git Bash console I see the following message:

但是,我注意到在我尝试了一个任意的git提交后,在我的Git Bash控制台中,我看到以下消息:

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

Unsure of what the ramifications of this might be.

不确定这可能是什么后果。

#5


GitPad sets your current text editor as the default editor for Git.

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

My default editor for .txt files in Windows 10 is Visual Studio Code and running GitPad once made it the default editor for Git. I haven't experienced the problems mentioned in the question (Git waits until VS Code window is closed in my case).

我在Windows 10中的.txt文件的默认编辑器是Visual Studio Code,并且运行GitPad曾经使它成为Git的默认编辑器。我没有遇到问题中提到的问题(在我的情况下,Git会等到VS Code窗口关闭)。

(The link for the .exe file didn't work for me, you may need to compile the source yourself.)

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

#6


Good news! At the time of writing, this feature has already been implemented in the 0.10.12-insiders release and carried out through 0.10.14-insiders. Hence we are going to have it in the upcoming version 1.0 Release of VS Code.

好消息!在撰写本文时,此功能已在0.10.12内部人员版本中实施,并通过0.10.14内部人员执行。因此,我们将在即将发布的VS Code 1.0版本中使用它。

Implementation Ref: Implement -w/--wait command line arg

实现参考:实现-w / - 等待命令行arg

#7


Run this command in your Mac Terminal app

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

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

#8


Just want to add these back slashes to previous answers, I am on Windows 10 CMD, and it doesn't work without back slashes before the spaces.

只想在前面的答案中添加这些反斜杠,我在Windows 10 CMD上,并且在空格之前没有反斜杠它不起作用。

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

#9


Im not sure you can do this, however you can try these additions in your gitconfig file.

我不确定你能做到这一点,但你可以在你的gitconfig文件中尝试这些添加。

Try to replace the kdiff3 from these values to point to visual studio code executable.

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

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

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

#10


I set up Visual Studio Code as a default to open .txt file. And next I did use simple command: git config --global core.editor "'C:\Users\UserName\AppData\Local\Code\app-0.7.10\Code.exe\'". And everything works pretty well.

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

#11


on windows 10 using the 64bit insiders edition the command should be:

在Windows 10上使用64位内部人员版本命令应该是:

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

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

you can also rename the 'code-insiders.cmd' to 'code.cmd' in the 'Program Files' directory, in this way you can now use the command 'code .' to start editing the files on the . directory

你也可以在'Program Files'目录中将'code-insiders.cmd'重命名为'code.cmd',这样你就可以使用命令'code'了。开始编辑文件。目录