2015-05-04 152 views
133

在命令行中使用git时,我想知道是否可以使用Visual Studio Code作为默认编辑器,即创建提交注释并查看diff来自命令行的文件。如何使用Visual Studio代码作为Git的默认编辑器

据我所知,它不可能用于合并(至少在一分钟内),但是有谁知道是否有可能使用它来查看diff,如果可以的话,哪些命令行选项在.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]> 

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

更新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茶回升米,所以我期待将来的版本将包括它。

UPDATE 4:

感谢@ F-boucheros下面的评论,我已经能够得到VS代码工作作为提交意见的默认编辑器,重订,等我还是想看看它也可以用作diff工具。

更新5:

中为每个问题接受的答案,使用的代码V1.0发布,这是现在可能。

回答

216

在最新的版本(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.
      • 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. enter image description here 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
+13

我还发现addinng'--new-window'对命令有帮助。这样git操作将在新窗口中打开。 – jrotello

+0

这个答案很好。唯一的问题是在当前的命令提示符窗口下打开代码窗口。 (运行代码版本1.4.0) –

+2

当我们谈论_command line_时,我们的意思是'Ctrl + Shift + P'?我的工作不正常(没有'code --help'可用),我不明白是什么意思:“确保在安装过程中选择了添加到PATH”我应该怎么做来检查它? 有人可以帮我吗? –

1

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

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

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

+0

正如我在我的问题提到的,我不认为合并将得到支持,如我不认为Visual Studio Code知道如何做到这一点,我怀疑它只能做差异和提交消息。 –

1

我打开了我的.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 

不确定。

+0

我也试过,但是,我得到了“致命的:在C:\ Users \ gep13/.gitconfig中的坏配置文件行14”,然后我注意到我有反斜杠,你有正斜杠。改变这些轮使它工作,但像你我看到“渲染器进程启动”输出,其实际输出两次,然后显示:“[11956:0504/091108:错误:ipc_channel_win.cc(136)]管道错误:109“,但提交实际上似乎已经奏效。 –

+0

啊,有趣。看起来我的建议只有在完成提交之前已经完全关闭代码_(不太方便!)_才有效。如果代码处于打开状态,我遇到了您在更新中报告的相同错误。 – miqid

+0

确实很有趣。我想这就是Notepad ++的-multiInst标志会起作用的地方。我想知道VSCode是否有类似的东西。 –

2

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

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

(该链接为.exe文件,我没有工作,你可能需要自己编译源代码。)

+0

谢谢!我会试试这个! :-) –

+0

你指定什么作为默认编辑器?我试图使用这里描述的内容:http://www.donovanbrown.com/post/2015/07/07/how-to-set-visual-studio-code-as-your-default-editor但是,那doesn似乎不想工作。真的不想指向一个特定的exe,因为这会随着代码的更新而改变。 –

+1

@Gary,我没有必要指定一个特定的文件,当我点击''右键''''打开'>'选择另一个应用程序'时,代码就在列表中。我希望它能够[注册自己](https://msdn.microsoft.com/en-us/library/windows/desktop/ee872121(v = vs.85).aspx)on setup(并且每次更新注册表更新所以shell可以找到它),但我找不到除注册表中的“打开代码”上下文菜单项之外的任何东西。所以我真的没有更清楚的答案,对不起。 –

0

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

17

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

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

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

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

下一次你做一个git rebase -i HEAD~3它会弹出Visual Studio代码。一旦VSCode关闭,Git将收回领先优势。

注:我当前版本的VSCode是0.9.2

我希望有所帮助。

+0

谢谢! '-w'的意义是什么,你在哪里发现有记录? –

+1

好点。它来自Atom -w是等待。我编辑了我的答案。 –

+0

我已经设置好了,我很高兴地说它正在工作。如果我现在可以像Visual Studio Code一样对这些文件进行差异化,那就太棒了! :-) –

1

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

实施编号:Implement -w/--wait command line arg

+0

你能否评论我将如何使用VSCode中的差异查看器来查看git中的差异? –

0

运行此命令在你的Mac终端应用

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

由于您收到错误消息,因此不太适用:由于提交消息为空而中止提交。需要添加等待标志。例如 - 等待。 – Belfield

相关问题