在 Visual Studio 中为 Microsoft Git Provider 设置代理 [英] Set proxy for Microsoft Git Provider in Visual Studio

查看:14
本文介绍了在 Visual Studio 中为 Microsoft Git Provider 设置代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用 http 代理连接到 Git 服务器.我可以通过 Git Bash 设置它并通过以下命令使用它:

git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:8080

但是,我使用 Microsoft Git Provider 与 Visual Studio 的集成.而且我无法在任何地方设置代理以连接到 Git 服务器.有没有办法在 Visual Studio 中保存 Microsoft Git Provider 的代理详细信息?

解决方案

在 Visual Studio 中没有直接设置 Git 代理的方法

你不需要在 Visual Studio 中设置任何东西来设置 Git 代理——事实上,我还没有找到任何直接在 Visual Studio 中这样做的方法,以及使用 devenv.exe 的替代答案.我个人无法开始工作.


但是,有一个简单的解决方案

只要您在安装过程中勾选了 Git,Visual Studio 就会为 Windows 安装 Git(最新版本默认有这个).安装 Windows 版 Git(或任何操作系统上的一般 Git)后,您可以直接在任何命令行、控制台或 Powershell 窗口中轻松设置全局 Git 代理设置.

实际上,您可以使用 Tools/NuGet Package Manager/Package Manager Console 在 Visual Studio 中直接打开命令或 Powershell 提示符.

如果安装了 Git,您可以在任何命令行输入 git,您将获得所有 git 命令的列表.如果没有发生这种情况,您可以直接为 Windows 安装 Git - 我建议将其作为安装 Git 扩展 GUI 应用程序的一部分进行安装,但您的里程可能会有所不同.

您特别需要的 git 命令是:

<块引用>

git config --global http.proxy http://USER:PASSWORD@URL:PORTgit config --global https.proxy http://USER:PASSWORD@URL:PORT

地点:

  • 代理地址可能是http://不是 https://
  • USER:PASSWORD@ 是您的代理需要的用户名和密码
  • URL 是代理的完整域名
  • PORT 是代理的端口,http 和 https 可能不同

这将在MyDocuments"文件夹中的全局配置文件中设置您的代理.根据您的操作系统和其他因素,文件可能会以不同的方式命名或放在其他地方.您始终可以使用以下命令直接查看此文件并编辑部分和键/值对:

<块引用>

git config --global -e

这将在 Git 的当前编辑器设置中打开全局配置,或者可能是系统默认文本编辑器.您还可以通过位于 repo 目录中并去掉 --global 标志来查看任何给定 repo 的配置文件.

设置代理后,您应该会在文件中看到类似以下内容:

<块引用>

[http]代理 = [https]代理 = 

您可以直接输入这些值而不是使用配置命令,也可以删除它们以从配置中删除代理.

注意:该文件也是用于提交的 user.name 和 user.email 的存储位置 - 请参阅 [user] 部分.


其他有用的代理 Git 配置


1. 如果你想为当前本地 repo 设置代理,你也可以去掉 --global 或用 --local 替换它(你必须在发出命令时的repo目录).


2. 此外,您可以为特定 URL 设置代理,如下所示:

<块引用>

git config --global http.<应用代理的完整 URL>.proxy git config --global https.<应用代理的完整 URL>.proxy 

注意应该使用完整的 URL(即 http://https:// 在前面).


3、另外,如果你有多个远程repos,比如originupstream,需要不同的代理,你可以专门设置一个代理.

<块引用>

git config --global http.upstream.proxy git config --global https.upstream.proxy <http://user:pass@url:port>


4. 您可以通过将代理 URL 替换为 "" 来将代理设置为空.例如,如果您想全局设置代理,然后排除位于公司防火墙后面的特定 URL(例如企业、本地版本的 Github),并且代理不处理本地地址正确.这对于本地主机和其他特殊地址或直接 IP 地址也可能有帮助.


5. 您可以使用以下命令检查给定 URL 的代理:

<块引用>

git config --get-urlmatch http.proxy <any random full URL>

例如:

<块引用>

git config --get-urlmatch http.proxy https://github.com

I have to use http proxy to connect to Git server. I am able to set it through Git Bash and use it too through the following command:

git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:8080

However, I am using Microsoft Git Provider integration with Visual Studio. And I am not able to set proxy anywhere to connect to Git server. Is there a way that I can save the proxy details for Microsoft Git Provider in Visual Studio?

解决方案

There is not a direct way to set a Git proxy in Visual Studio

You don't need to setup anything in Visual Studio in order to setup the Git proxy - in fact, I haven't found any way to do so within Visual Studio directly, and the alternate answer on using devenv.exe.config I was not personally able to get to work.


However, there is an easy solution

Visual Studio will install Git for Windows as long as you have Git checkmarked during install (the latest versions have this by default). Once Git for Windows (or Git in general on any OS) is installed, you can easily setup the global Git proxy settings directly at any command line, console, or Powershell window.

In fact, you can open a command or Powershell prompt directly in Visual Studio with Tools/NuGet Package Manager/Package Manager Console.

If Git is installed, you can type git at any command line and you'll get a list of all the git commands. If this does not happen, you can install Git for Windows directly - I would recommend doing that as a part of installing the Git Extensions GUI application, but your mileage may vary.

The git commands in particular you need are:

git config --global http.proxy http://USER:PASSWORD@URL:PORT
git config --global https.proxy http://USER:PASSWORD@URL:PORT

Where:

  • The proxy address likely is http:// and not https://
  • USER:PASSWORD@ is the user name and password if required for your proxy
  • URL is the full domain name of the proxy
  • PORT is the port of the proxy, and might be different for http and https

This will setup your proxy in a global config file in your "MyDocuments" folder. The file might be named differently or place somewhere else depending on your OS and other factors. You can always view this file and edit the sections and key/value pairs directly with the following command:

git config --global -e

This will open up the global config in the current editor setup in Git, or possibly the system default text editor. You can also see the config file for any given repo by being in the repo directory and leaving off the --global flag.

After setting up the proxy, you should see something like the following as a part of the file:

[http]
    proxy = <http://user:pass@url:port>
[https]
    proxy = <http://user:pass@url:port>

You can enter these values directly rather than using the config commands, or can delete them to remove the proxy from the config.

Note: This file is also where the user.name and user.email that are used for commits is stored - see the [user] section.


Other useful Git configs for proxy


1. You can also leave off the --global or replace it with --local if you want to setup the proxy for the current local repo (you must be in the repo directory when issuing the command).


2. In addition, you can setup a proxy for just a specific URL as follows:

git config --global http.<full URL to apply proxy>.proxy <http://user:pass@url:port>
git config --global https.<full URL to apply proxy>.proxy <http://user:pass@url:port>

Note that the full URL should be used (i.e. http:// or https:// in the front).


3. In addition, if you ever have multiple remote repos, say origin and upstream, which need different proxies, you can set a proxy for one specifically.

git config --global http.upstream.proxy <http://user:pass@url:port>
git config --global https.upstream.proxy <http://user:pass@url:port>


4. You can set the proxy to null by substituting "" for the proxy URL. This may be useful if, for example, you want to set the proxy globally, but then exclude a specific URL which is behind your company firewall (such as an enterprise, on premises version of Github), and the proxy doesn't handle local addresses correctly. This may also be helpful with localhost and other special addresses or direct IP addresses.


5. You can check what the proxy is for a given URL with the following:

git config --get-urlmatch http.proxy <any random full URL>

for example:

git config --get-urlmatch http.proxy https://github.com

这篇关于在 Visual Studio 中为 Microsoft Git Provider 设置代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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