關(guān)于git代理的使用姿勢
上午用git下載兩個(gè)帶測試集、訓(xùn)練集的AI工程,不使用Proxy,確實(shí)給惡心的不要不要的。。。
通常使用代理主要原因無非有二:
訪問速度實(shí)在令人發(fā)指 unable to access '...' Couldn't resolve host '...'的情況,沒辦法只能科學(xué)上網(wǎng)解決
正常我們從github獲取repo通常是這樣的
Cloning into 'xxx'...
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (13/13), done.
remote: Compressing objects: 100% (9/9), done.
Receiving objects: 16% (359/2241), 100.01 KiB | 29.00 KiB/s
下載速度只有29kib/s,對于稍微大點(diǎn)兒的repo來說顯然等的讓人焦心。好在一般工作都會(huì)提供一些代理,用上代理訪問github repo速度就有了質(zhì)的飛躍:
Cloning into 'xxx'...
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (13/13), done.
remote: Compressing objects: 100% (9/9), done.
Receiving objects: 96% (2171/2241), 10.05 MiB | 4.85 MiB/s
好比單車變火箭??。
因?yàn)橹笆褂玫拇肀容^粗暴裸著沒有認(rèn)證,所以使用起來比較簡單:
alias proxy='export all_proxy=http://yourproxydomain:port'
或者你想精細(xì)化區(qū)分http和https代理
alias httpProxy='export http_proxy=http://yourproxydomain:port'
alias httpsProxy='export https_proxy=http://yourproxydomain:port'
都不需要進(jìn)行git config配置。不過最近裸著的代理終于給加上認(rèn)證了,直接使用上面的方式來download github的repo就不好使了。
既然有了認(rèn)證,那么使用代理自然需要用戶名密碼,上面代理需要改成如下配置:
alias proxy='export all_proxy=http://username:password@yourproxydomain:port'
alias httpProxy='export http_proxy=http://username:password@yourproxydomain:port'
alias httpsProxy='export https_proxy=http://username:password@yourproxydomain:port'
有了上面配置訪問網(wǎng)址啥的沒啥問題,但是git clone時(shí)會(huì)失?。?/p>
Cloning into 'xxx'...
fatal: unable to access 'https://github.com/xxx/xxx.git/': Proxy CONNECT aborted
因?yàn)橛辛苏J(rèn)證代理,git需要設(shè)置下認(rèn)證方法為basic
git config --global http.proxyAuthMethod 'basic'
這樣使用終端代理配置加上git代理方法的設(shè)置問題就解決了。以上這些僅是個(gè)人的配置,如果你覺得麻煩,網(wǎng)上也有很多插件供你選擇使用。
當(dāng)然git也專門提供了關(guān)于代理配置,我們可以不設(shè)置終端代理讓git自己走代理,接下來一起來看看吧
git代理配置
git通常支持https和ssh兩種協(xié)議,因?yàn)槭褂脜f(xié)議不同,它們代理的配置也有很大差異。
Http配置代理
git http代理的配置比較簡單,一條命令即可解決:
git config --global http.proxy http://proxyUsername:[email protected]:port
或者你也可以對指定域名進(jìn)行代理:
git config --global http.https://domain.com.proxy http://proxyUsername:[email protected]:port
git config --global http.https://domain.com.sslVerify false
如果你使用了中間人Https代理,需要將http.<url>.sslVerify設(shè)置為false?;蛘撸憧梢垣@取證書鏈的根CA,并使用http.sslCAInfo或http.sslCAPath來指定。
上面這些命令會(huì)最終記錄在~/.gitconfig文件中:
[http]
[http "https://domain.com"]
proxy = http://proxyUsername:[email protected]:port
sslVerify = false
當(dāng)你遇到unable to access 'https://...': Unknown SSL protocol error in connection to ...:443你也可以針對當(dāng)前repo操作單獨(dú)使用-c http.sslVerify=false設(shè)置ssl檢驗(yàn)開關(guān)。
git -c http.sslVerify=false clone https://domain.com/path/to/git
一旦repo克隆成功,你也可以針對當(dāng)前repo設(shè)置sslVerify,其設(shè)置結(jié)果作用于當(dāng)前repo的.git/config中,注意這里沒有--config
git config http.sslVerify false
查看當(dāng)前的配置
展示當(dāng)前全局http配置,即~/.gitconfig中的http部分
git config --global --get-regexp http
取消代理或ssl verification配置
使用--unset可刪除特定屬性的配置。如取消http.proxy或http.sslVerify:
git config --global --unset http.proxy
git config --global --unset http.https://domain.com.proxy
git config --global --unset http.sslVerify
git config --global --unset http.https://domain.com.sslVerify
ssh代理配置
git ssh的代理配置保存在~/.ssh/config中,如果沒有可以創(chuàng)建一個(gè)。配置格式如下:
對于需要認(rèn)證的代理需要安裝下
ncat。mac下brew install nmap即可。
# 例如github.com的配置
# User、Hostname、Port 為ssh登錄的服務(wù)器配置
# 注意的是這里的 User 應(yīng)該是 git,而不是你在該網(wǎng)站上注冊的用戶名。 其他網(wǎng)站可以根據(jù)具體情況設(shè)置
Host github.com
User git
Hostname github.com
Port 22
Proxycommand /usr/bin/ncat --proxy proxy.server.com:port --proxy-type http --proxy-auth proxyUsername:proxyPassword %h %p
如果Host設(shè)置為*,則此配置會(huì)應(yīng)用到?jīng)]有獨(dú)立配置的ssh連接中。--proxy-type可以設(shè)置代理使用的協(xié)議,如:http ,socks4,socks5
這樣就可以愉快使用代理了。當(dāng)然感興趣也可以man git-config查閱,相信很多問題可以迎刃而解。
如果閱讀過程中發(fā)現(xiàn)本文存疑或錯(cuò)誤的地方,可以關(guān)注公眾號留言。如果覺得還可以 幫忙點(diǎn)個(gè)在看??
