Created
January 23, 2013 12:43
-
-
Save dexteryy/4605134 to your computer and use it in GitHub Desktop.
如何让命令行工具通过代理正常访问github
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
首先你需要一个不怕dns污染的http代理,比如连接到私人VPS的ssh tunnel+privoxy之类… | |
以下例子里我都使用10.8.0.1:8118作为代理服务器。 | |
## 让普通命令行工具使用代理 | |
可以在~/.profile或~/.bashrc里加上: | |
``` | |
enable_proxy() { | |
export http_proxy="10.8.0.1:8118" | |
export https_proxy="10.8.0.1:8118" | |
} | |
disable_proxy() { | |
unset http_proxy | |
unset https_proxy | |
} | |
``` | |
更好的方法是给单个命令加上局部的代理设置,比如我很早就给pip加了… | |
``` | |
alias pip='http_proxy="10.8.0.1:8118" https_proxy="10.8.0.1:8118" pip' | |
``` | |
快速测试: | |
``` | |
http_proxy="10.8.0.1:8118" curl -I http://github.com | |
``` | |
## 让git fetch使用代理 | |
很多命令行工具特别是包管理工具,比如homebrew和[istatic](https://github.com/mockee/istatic),都会用git请求github的url,brew用的是http://协议,istatic用的是git://协议(ssh) | |
有三种方法让这种请求通过代理: | |
1. git config --global http.proxy 10.8.0.1:8118 | |
2. git config --global core.gitproxy "proxy-command" | |
3. export GIT_PROXY_COMMAND="proxy-command" | |
后两种可以结合[socat](http://www.dest-unreach.org/socat/)来支持git://协议 | |
``` | |
brew install socat | |
``` | |
在~/.profile或~/.bashrc里加上 | |
``` | |
enable_git_proxy() { | |
export GIT_PROXY_COMMAND=/tmp/gitproxy; | |
cat > $GIT_PROXY_COMMAND <<EOF | |
#!/bin/bash | |
exec socat STDIO PROXY:10.8.0.1:\$1:\$2,proxyport=8118 | |
EOF | |
chmod +x $GIT_PROXY_COMMAND; | |
} | |
enable_git_proxy | |
``` | |
## 让git push [email protected]支持代理 | |
为了避免每次git push origin都输入用户名和密码,需要把.git/config里的https地址改成ssh的,比如: | |
``` | |
[remote "origin"] | |
url = https://[email protected]/UserName/MyRepo.git | |
``` | |
改成: | |
``` | |
[remote "origin"] | |
url = [email protected]:UserName/MyRepo.git | |
``` | |
这样会导致上面设置GIT_PROXY_COMMAND的方法失效 | |
解决方法是在~/.ssh/config里加入: | |
``` | |
host github | |
user git | |
hostname github.com | |
port 22 | |
proxycommand socat STDIO PROXY:10.8.0.1:%h:%p,proxyport=8118 | |
``` | |
然后把.git/config里的url写成: | |
``` | |
[remote "origin"] | |
url = github:UserName/MyRepo.git | |
``` | |
快速测试: | |
``` | |
ssh -T github | |
``` | |
参考: | |
[http://www.kernel.org/pub/software/scm/git/docs/git-config.html](http://www.kernel.org/pub/software/scm/git/docs/git-config.html) | |
[http://www.gromacs.org/Developer_Zone/Git/Git_Tutorial#Git_behind_a_proxy](http://www.gromacs.org/Developer_Zone/Git/Git_Tutorial#Git_behind_a_proxy) | |
[http://sitaramc.github.com/tips/git-over-proxy.html](http://sitaramc.github.com/tips/git-over-proxy.html) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment