LoginSignup
9
7

More than 5 years have passed since last update.

プロキシ内/外両方のGitリポジトリにアクセスする設定

Last updated at Posted at 2017-01-13

背景

GitHubやBitBucketなどのGitリポジトリに、プロキシ経由でアクセスしたいとします。

MyPC ---> Proxy ---> GitHub

この場合、通常git configでプロキシの設定を行います。

git config --global http.proxy http://proxy.example.com:8080
git config --global https.proxy http://proxy.example.com:8080

しかし、この設定をすると、gitがプロキシを使おうとするため、
プロキシを使わない内部のGitリポジトリにアクセスできなくなります。

MyPC ---> Proxy --✕-> MyRepo (見つからない)

ほんとうはこうなって欲しいわけです。

MyPC --+--> Proxy ---> GitHub
       |
       +--> MyRepo 

社内に、GitBucket等で構築したGitリポジトリが存在する場合、
こういう状態になると思います。

設定方法その1 リポジトリURL毎にプロキシ設定する(オススメ)

リモートリポジトリ(GitHub, BitBucket等)のURL毎にプロキシを設定します。

git config --global http.https://github.com/.proxy http://proxy.example.com:8080
git config --global http.https://bitbucket.org/.proxy http://proxy.example.com:8080

これで、~/.gitconfigに以下のように追記されます。

[http "https://bitbucket.org/"]
    proxy = http://proxy.example.com:8080
[http "https://github.com/"]
    proxy = http://proxy.example.com:8080

この状態だと、登録したリポジトリであれば、何もしなくてもgit cloneできました。
また、プロキシを経由したくないリポジトリもふつうにgit cloneできました。

プロキシ経由で使いたいリポジトリはほぼ決まっていることが多いと思います。
その場合は、この設定が簡単です。

設定方法その2 ローカルリポジトリにプロキシ設定する

ローカルリポジトリの.git/configにプロキシを設定します。

git clone

最初にcloneする際は、環境変数を一時的に設定して、git cloneします。

# 一時的にプロキシ設定をします
set http_proxy=proxy.example.com:8080 & set https_proxy=proxy.example.com:8080

# この状態でクローンします
git clone https://github.com/xxx/myrepo.git

これでプロキシ経由でcloneできると思います。

git config --local

ローカルリポジトリにプロキシの設定を追加します。
--globalではなく、--localとするところがポイントです。

# cloneしたローカルリポジトリに移動します。
cd myrepo

# ローカルリポジトリにプロキシの設定を追加します。
git config --local http.proxy http://proxy.example.com:8080
git config --local https.proxy http://proxy.example.com:8080

これで、このローカルリポジトリで作業する場合は
プロキシが使用されるようになります。

おわりに

ほとんどの場合は「その1」のやり方で良いと思います。
プロキシを使うことが稀である場合なら、globalな設定をしない「その2」も
良いかと思います。

9
7
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
7