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

我知道不可能使用它来做合并(至少在一分钟),但有人知道是否有可能使用它来查看diff,如果是这样,在.gitconfig文件中需要什么命令行选项来实现这一点吗?

更新1:

我尝试了一种类似于我过去为notepad++所做的方法,即。

#!/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]>

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

更新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:

我已经得到可靠的消息,VSCode团队已经选择了这个功能,所以我期待着未来的版本能包含它。

更新4:

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

更新5:

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


当前回答

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

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

这利用了新的——diff选项,你可以传递给VS Code 并排比较两个文件。 总结一下,这里有一些可以在VS中使用Git的例子 代码: git rebase HEAD~3 -i允许使用VS Code进行交互rebase git commit允许使用VS Code来提交消息 Git add -p后跟e用于交互式添加 git difftool <commit>^ <commit>允许使用VS Code作为diff编辑器进行更改

其他回答

好消息!在撰写本文时,该特性已经在0.10.12 insiders版本中实现,并通过0.10.14 insiders实现。因此,我们将在即将发布的VS Code 1.0版本中使用它。

实现参考:实现-w/——wait命令行参数

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

尝试将这些值中的kdiff3替换为指向visual studio代码可执行文件。

(合并) 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 Bash控制台尝试了任意git提交后,我看到以下消息:

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

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

在.gitconfig文件中需要哪些命令行选项才能实现这一点?

为了让git-lens工作,我必须把这个添加到我的.gitconfig文件中:


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

-n是可选的-正如Alex下面提到的,它是——new-window的缩写,我发现它很有用:)

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

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

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