介绍
    目前,网络上可选择的Git远程仓库比较多,其中用的较多的可能就是github和bitbucket(当然,你也可以使用自己搭建的远程仓库)。github和bitbucket的主要区别在于:bitbucket创建私人库是免费的。如果你不介意自己的代码公开,那你就可以使用github。如果,你有些私人的代码的话,又需要版本控制,这时候bitbucket就满足需要了。
    但是,在国内的主要问题是:网络不稳定。这样,就会经常发生git不能push的情况。所以,这时候如果你有个代理服务器,就可以通过设置使git通过代理访问远程仓库,达到家和公司代码同步的目的。
    Git允许使用三种协议(http://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols来连接远程仓库:ssh、http、git。所以,如果你要设置代理,必须首先明确本地git使用何种协议连接远程仓库,然后根据不同协议设置代理。

前提
    socks5代理服务器,默认端口1080

设置SSH协议的代理
如果你的远程仓库拥有如下的格式:

点击(此处)折叠或打开

  1. git@github.com:archerie/learngit.git
  2. ssh://git@github.com:archerie/learngit.git

那么,你使用的是SSH协议连接的远程仓库。因为git依赖ssh去连接,所以,我们需要配置ssh的socks5代理实现git的代理。在ssh的配置文件~/.ssh/config(没有则新建)使用ProxyCommand配置:

点击(此处)折叠或打开

  1. #Linux
  2. Host bitbucket.org
  3. User git
  4. Port 22
  5. Hostname bitbucket.org
  6. ProxyCommand nc -x 127.0.0.1:1080 %h %p

点击(此处)折叠或打开

  1. #windows
  2. Host bitbucket.org
  3. User git
  4. Port 22
  5. Hostname bitbucket.org
  6. ProxyCommand connect -S 127.0.0.1:1080 %h %p

如果你使用github,那么你只需要把bitbucket.org换成github.com就行了。具体配置的含义请参考ssh_config(5)

设置http/https协议代理

如果你的远程仓库链接拥有如下格式:

点击(此处)折叠或打开

  1. http://github.com/archerie/learngit.git
  2. https://github.com/archerie/learngit.git

说明你使用的是http/https协议,所以可以使用git配套的CMSSW支持的代理协议:SOCKS4、SOCKS5和HTTPS/HTTPS。可通过配置http.proxy配置:

点击(此处)折叠或打开

  1. # 全局设置
  2. git config --global http.proxy socks5://localhost:1080
  3. # 本次设置
  4. git clone https://github.com/example/example.git --config "http.proxy=127.0.0.1:1080"

这里演示的是socks5的配置,需要其他配置的可参考git-config配置中的http.proxy。

设置Git协议的代理

Git协议是Git提供的一个守护进程,它监听专门的端口(9418),然后提供类似于ssh协议一样的服务,只是它不需要验证。所以,然后用户通过网络都可以使用git协议连接提供git连接的仓库。如果远程仓库的链接是如下形式:

点击(此处)折叠或打开

  1. git://github.com/archerie/learngit.git

那么,该仓库使用git协议连接。所以,需要使用CMSSW提供的简单脚本去通过socks5代理访问:git-proxy。配置如下:

点击(此处)折叠或打开

  1. git config --global core.gitproxy "git-proxy"
  2. git config --global socks.proxy "localhost:1080"

了解更多,使用git-proxy --help。

参考链接

http://cms-sw.github.io/tutorial-proxy.html
http://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols
http://stackoverflow.com/questions/783811/getting-git-to-work-with-a-proxy-server



转载自:http://m.blog.csdn.net/w1196726224/article/details/50252929







09-10 10:23