環境変数でgitのプロキシ設定するなら Git Bashとコマンドプロンプトでやり方が違うよ


概要

「Windowsで、コマンドプロンプトとGit Bashを同時に使かもしれない人」かつ「環境変数でgitのプロキシ設定する人」という狭い範囲向けの話。

結論

環境変数でgitのプロキシ設定するなら、

  • コマンドプロンプトは、setで環境変数の設定
  • Git Bashは、exportで環境変数の設定

ということを思い出す・・・もしくは、

一時的にproxyを有効_無効にしてgit cloneする方法 - Qiita

git clone <repository-url> -c http.proxy="http://<proxyserver>:<port>" -c https.proxy="https://<proxyserver>:<port>"

を使おう。

以下、詳細

詳細

コマンドプロンプト、Cmder、バッチファイルでは以下のようにしてました。

> set http_proxy=http://<proxyserver>:<port>
> set https_proxy=https://proxyserver>:<port>
> git clone <repository-url>

普段あまり使わないGit Bashを使ってみたら・・・

$ set http_proxy=http://<proxyserver>:<port>
$ set https_proxy=https://proxyserver>:<port>
$ git clone <repository-url>
Cloning into '<repository>'...
fatal: unable to access '<repository-url>': The requested URL returned error: 403

あら?cloneできない。

「[http経由の git コマンド(push,clone,fetch..)等がなんかうまく動かないときにデバッグする方法2」を試そう。

$ GIT_CURL_VERBOSE=1 git clone <repository-url> 
Cloning into '<repository>'...
* Couldn't find host <repository-host-server> in the _netrc file; using defaults
*   Trying <repository-host-server> ...
...略...
* Connected to <repository-host-server> (<ipaddress>) port 80 (#0)
> GET <repository-url>/info/refs?service=git-upload-pack HTTP/1.1
Host: <repository-host-server>
...略...
< HTTP/1.1 403 Forbidden
...略...
* Connection #0 to host <repository-host-server> left intact
fatal: unable to access '<repository-url>': The requested URL returned error: 403

HTTP/1.1 403 Forbiddenが返ってきてる。

設定したはずの<proxyserver>が全く登場してない。

setが効いてない?

Set an environment variable in git bash - Stack Overflow

You can combine the assignment with the export statement.
export HOME=c

Set an environment variable in git bash - Stack Overflow

(export if I am on Unix, or a simple set on Windows)

あ。Git「Bash」か。

あと、コマンドプロンプトは、"http://<proxyserver>:<port>"のようにダブルクオーテーションで囲むとエラーになるけど、

> set http_proxy="http://<proxyserver>:<port>"
> set https_proxy="https://proxyserver>:<port>"
> git clone <repository-url>
fatal: unable to access '<repository-url>': Unsupported proxy syntax in '<proxyserver>:<port>"'

Git Bashはエラーにならないよ。

$ export http_proxy="http://<proxyserver>:<port>"
$ export https_proxy="https://<proxyserver>:<port>"
$ git clone <repository-url>
Cloning into '<repository>'...
remote: Counting objects: 50, done.
remote: Compressing objects: 100% (45/45), done.
remote: Total 50 (delta 13), reused 0 (delta 0)
Unpacking objects: 100% (50/50), done.