リポジトリのアクセスの際にプロキシサーバーが必要な場合の設定手順 - Git

リポジトリのアクセスの際にプロキシサーバーが必要な場合の設定手順を紹介します。

概要

GitHubなどのインターネットの接続が必要なリモートリポジトリを利用している場合、 ネットワーク構成によってはアクセスの際にプロキシサーバーが必要な場合があります。 GitではOSのプロキシ設定を参照しないため、プロキシが必要な場合は別途設定をする必要があります。
この記事ではGitでプロキシサーバーを利用する場合の設定手順を紹介します。

手順

gitconfig または .gitconfig ファイルを編集するか git コマンドを実行します。

方法1: gitconfigファイルを修正

gitconfigファイルを修正します。
gitconfig ファイルの[http] [https]セクションに以下の記述を追加します。
認証が不要のプロキシの場合
[http]
    proxy = http://[プロキシサーバのアドレス]:[プロキシサーバのポート番号]
[https]
    proxy = http://[プロキシサーバのアドレス]:[プロキシサーバのポート番号]
認証が必要なプロキシの場合
[http]
    proxy = http://[ユーザ名]:[パスワード]@[プロキシサーバのアドレス]:[プロキシサーバのポート番号]
[https]
    proxy = http://[ユーザ名]:[パスワード]@[プロキシサーバのアドレス]:[プロキシサーバのポート番号]
注意
httpsの場合でも http://~ となることに注意してください。

gitconfigファイルの位置

gitconfigファイルは以下の場所に配置されています。
Git for Windowsを利用している場合
C:\Program Files\Git\etc\gitconfig
ユーザーディレクトリにGit for Windowsをインストールしている場合、
C:\Users¥(ユーザー名)\AppData\Local\Programs\Git\etc\gitconfig
VisualStudio のチームエクスプローラーを利用している場合
以下のgitconfigファイルを修正します。(Visual Studioのエディションによりファイルパスが若干異なります。)

Visual Studio 2022の例
C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\etc
Visual Studio 2019の例
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\etc\gitconfig

編集例

変更前 gitconfig
[core]
	symlinks = false
	autocrlf = true
[color]
	diff = auto
	status = auto
	branch = auto
	interactive = true
[pack]
	packSizeLimit = 2g
[help]
	format = html
[http]
	sslCAinfo = /ssl/certs/ca-bundle.crt
[diff "astextplain"]
	textconv = astextplain
[rebase]
	autosquash = true
[filter "lfs"]
	clean = git-lfs clean -- %f
	smudge = git-lfs smudge -- %f
	process = git-lfs filter-process
	required = true
[include]
	; include Git for Windows' system config in order
	; to inherit settings like `core.autocrlf`
	path = C:/Program Files (x86)/Git/etc/gitconfig
	path = C:/Program Files/Git/etc/gitconfig
変更後 gitconfig
[core]
	symlinks = false
	autocrlf = true
[color]
	diff = auto
	status = auto
	branch = auto
	interactive = true
[pack]
	packSizeLimit = 2g
[help]
	format = html
[http]
	sslCAinfo = /ssl/certs/ca-bundle.crt
	proxy = http://penguin:hopjump@192.168.0.64:8080
[https]
	proxy = http://penguin:hopjump@192.168.0.64:8080
[diff "astextplain"]
	textconv = astextplain
[rebase]
	autosquash = true
[filter "lfs"]
	clean = git-lfs clean -- %f
	smudge = git-lfs smudge -- %f
	process = git-lfs filter-process
	required = true
[include]
	; include Git for Windows' system config in order
	; to inherit settings like `core.autocrlf`
	path = C:/Program Files (x86)/Git/etc/gitconfig
	path = C:/Program Files/Git/etc/gitconfig

方法2: gitコマンドを実行

以下のコマンドを実行します。
認証が不要のプロキシの場合
git config --global http.proxy http://[プロキシサーバのアドレス]:[プロキシサーバのポート番号]
git config --global https.proxy http://[プロキシサーバのアドレス]:[プロキシサーバのポート番号]
認証が必要なプロキシの場合
git config --global http.proxy http://[ユーザ名]:[パスワード]@[プロキシサーバのアドレス]:[プロキシサーバのポート番号]
git config --global https.proxy http://[ユーザ名]:[パスワード]@[プロキシサーバのアドレス]:[プロキシサーバのポート番号]
注意
httpsの場合でも http://[ユーザ名]:[パスワード] となることに注意してください。
注意
パスワードに記号がある場合はエスケープが必要です。こちらの記事を参照してください。

記述例1

httpsプロキシとhttpsプロキシが同一の場合。 多くの環境では、httpとhttpsプロキシは同一の場合が多いです。
git config --global http.proxy http://192.168.0.160:8080
git config --global https.proxy http://192.168.0.160:8080
git config --global http.proxy http://duck:guagua@192.168.0.160:8080
git config --global https.proxy http://duck:guagua@192.168.0.160:8080

記述例2

httpsプロキシとhttpsプロキシが別ポートの場合
git config --global http.proxy http://192.168.0.160:8080
git config --global https.proxy http://192.168.0.160:8081
git config --global http.proxy http://penguin:ponpon@192.168.0.160:8080
git config --global https.proxy http://penguin:ponpon@192.168.0.160:8081
著者
iPentec.com の代表。ハードウェア、サーバー投資、管理などを担当。
Office 365やデータベースの記事なども担当。
掲載日: 2020-09-23
iPentec all rights reserverd.