如何使用 Visual Studio Code 作为 git 的默认编辑器? [英] How to use Visual Studio Code as default editor for git?

查看:66
本文介绍了如何使用 Visual Studio Code 作为 git 的默认编辑器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

更新 1:

我尝试了一种类似于我为 添加以下内容以支持使用 VS Code 作为差异工具:

[差异]工具 = 默认差异工具[差异工具默认差异工具"]cmd = 代码 --wait --diff $LOCAL $REMOTE

<块引用>

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

总而言之,以下是一些可以在 VS 中使用 Git 的示例代码:

  • git rebase HEAD~3 -i 允许使用 VS Code 进行交互式 rebase
  • git commit 允许将 VS Code 用于提交消息
  • git add -p 后跟 e 用于交互式添加
  • git difftool ^ 允许使用 VS Code 作为差异编辑器进行更改

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.

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?

UPDATE 1:

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

#!/bin/sh

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

And used:

#!/bin/sh

"C:Usersgep13AppDataLocalCodeapp-0.1.0Code.exe" "$*"

But this results in an error message:

C:	emp	estrepo [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:	emp	estrepo [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 :-(

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.

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.

UPDATE 5:

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

解决方案

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:

  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

这篇关于如何使用 Visual Studio Code 作为 git 的默认编辑器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆