简体   繁体   English

我可以在 Visual Studio 中运行 Git 命令吗?

[英]Can I run Git commands in Visual Studio?

I am using Visual Studio Team Explorer with git integration for source control.我正在使用带有 git 集成的 Visual Studio Team Explorer 进行源代码控制。 It works pretty well but sometimes I need to run some more advanced git commands so I am going to file explorer and opening git bash.它工作得很好,但有时我需要运行一些更高级的 git 命令,所以我要打开文件资源管理器并打开 git bash。 Can I run git commands inside Visual Studio?我可以在 Visual Studio 中运行 git 命令吗?

Using Visual Studio 19.使用 Visual Studio 19。

You can use the Package Manager Console (in the bottom tab in VS) to run Git commands.您可以使用 Package 管理器控制台(在 VS 的底部选项卡中)运行 Git 命令。 see here 看这里

The Git integration in Visual Studio does not offer any terminal or ability to manually run Git commands. Visual Studio 中的 Git 集成不提供任何终端或手动运行 Git 命令的能力。 Using other means to obtain a terminal (ex: Package Manager Console as mentioned above) is a workaround for some Git commands.使用其他方式获取终端(例如:如上所述的 Package 管理器控制台)是某些 Git 命令的解决方法。

You will run into some issues, though.但是,您会遇到一些问题。 For example, commands that present paged output (ex: Press key to continue) often do not work properly in the Package Manager Console.例如,显示分页 output 的命令(例如:按键继续)在 Package 管理器控制台中通常无法正常工作。 They may scroll all the way to the end of their results rather than breaking on page boundaries since they can't tell the size of the Package Manager Console tool window.他们可能会一直滚动到结果的末尾,而不是打破页面边界,因为他们无法判断 Package 管理器控制台工具 window 的大小。 There are some other little annoyances that you may encounter as well.您可能还会遇到其他一些小烦恼。

Git became the default revision control system with the update of Visual Studio 2019 to version 16.8 in November 2020. I'm aware of two main ways to start a git command prompt using 16.8 or newer: Git 在 2020 年 11 月将 Visual Studio 2019 更新到 16.8 版后成为默认版本控制系统。我知道使用 16.8 或更高版本启动 git 命令提示符的两种主要方法:

  1. Right click the solution and choose Open in Terminal.右键单击解决方案并选择在终端中打开。 This starts a PowerShell instance which docks (by default) at the bottom of Visual Studio and can be used to invoke system git.这将启动一个 PowerShell 实例,该实例停靠(默认)在 Visual Studio 的底部,可用于调用系统 git。
  2. Git -> Open Repository in -> Command Prompt from the main menu (the top level Git menu is between the View and Project menus) opens a cmd.exe instance which also uses the system git installation. Git -> Open Repository in -> Command Prompt from the main menu (the top level Git menu is between the View and Project menus) opens a cmd.exe instance which also uses the system git installation.

Other methods of access include the options under Tools -> Command Line in the main menu.其他访问方法包括主菜单中工具 -> 命令行下的选项。 Starting a git command prompt through Team Explorer was removed Visual Studio 16.8.在 Visual Studio 16.8 中删除了通过 Team Explorer 启动 git 命令提示符。 A functionality moved message is displayed instead.而是显示功能移动消息。

As of 16.9.1, Visual Studio does not check that a system git installation is present (in C:\Program Files\Git by default).从 16.9.1 开始,Visual Studio 不会检查系统 git 安装是否存在(默认情况下在 C:\Program Files\Git 中)。 If no system git has been installed by the Git for Windows Visual Studio component or some other tool neither of the pathways described here will work.如果 Git 没有为 Windows Visual Studio 组件或其他一些工具安装系统 git,则此处描述的路径都不起作用。 Thus, this part of Visual Studio's functionality can potentially be broken out of the box.因此,Visual Studio 的这部分功能可能是开箱即用的。

Visual Studio 2013 Update 1 through Visual Studio 2019 version 16.7 have a git client built into Team Explorer.从 Visual Studio 2013 Update 1 到 Visual Studio 2019 版本 16.7,团队资源管理器中内置了 git 客户端。 To locate the feature, open a project that's controlled by Git (or just git init an existing project), and select View->Team Explorer from the menu.要找到该功能,请打开由 Git(或仅 git 启动现有项目)控制的项目,然后从菜单中打开 select 查看->团队资源管理器 You'll see the " Connect " view, which looks a bit like this:你会看到“ Connect ”视图,看起来有点像这样:

从团队资源管理器连接到 Git 存储库

You can read more about it here: https://devblogs.microsoft.com/devops/use-the-git-command-prompt-to-supplement-visual-studio/您可以在此处阅读有关它的更多信息: https://devblogs.microsoft.com/devops/use-the-git-command-prompt-to-supplement-visual-studio/

Update: Visual Studio 2022更新: Visual Studio 2022

  • Open Visual Studio 2022打开 Visual Studio 2022
  • Find git options from top left 4从左上角找到git选项 4
  • Click Open in command prompt Open in command prompt

Basically it opens a cmd command prompt but it will work as git.基本上它会打开一个cmd命令提示符,但它将作为 git 工作。 Check git --version if you have already installed any version of git or not.如果您已经安装了 git 的任何版本,请检查git --version

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM