Set proxy for Microsoft Git Provider in Visual Stu

2019-02-09 08:25发布

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?

3条回答
Anthone
2楼-- · 2019-02-09 08:34

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
查看更多
小情绪 Triste *
3楼-- · 2019-02-09 08:46

If help someone the VS 2017 has an folder(%ProgramFiles%\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\mingw32\etc) with a file named "gitconfig" as the file in "C:\Users[User].gitconfig" where you can set the proxy configuration for VS.

查看更多
甜甜的少女心
4楼-- · 2019-02-09 09:00

You can set a proxy for Visual Studio as in https://msdn.microsoft.com/en-us/library/dn771556.aspx :

Find devenv.exe.config (the devenv.exe configuration file) in: %ProgramFiles%\Microsoft Visual Studio 14.0\Common7\IDE (or %ProgramFiles(x86)%\Microsoft Visual Studio 14.0\Common7\IDE).

In the configuration file, find the <system.net> block, and add this code: XML

<defaultProxy enabled="true" useDefaultCredentials="true">  
    <proxy bypassonlocal="True" proxyaddress=" HYPERLINK "http://<yourproxy:port#" http://<yourproxy:port#>"/>  
</defaultProxy>  

You must insert the correct proxy address for your network in proxyaddress="http://.

查看更多
登录 后发表回答