git 代理配置

github 在国内访问一直不是很稳定,clone/push 等操作速度很慢。以下提供 https 和 ssh 这两种的访问方式的加速方法的配置,以及优缺点比较。

https

github 允许用户通过 https 端口使用 ssh,可以通过下面的指令测试

ssh -T -p 443 git@ssh.github.com
Hi username! You've successfully authenticated, but GitHub does not
provide shell access.

如果测试不通过,就需要修改配置文件 ~/.ssh/config,增加

Host github.com
  Hostname ssh.github.com
  Port 443

优点

  • httphttps 代理是非常常见的,比如我一般都是对系统全局代理
  • 配置比较简单

缺点

更多详情参考 Using SSH over the HTTPS port - User Documentation

ssh

前提:需要一个 socks5 代理

如果是直接通过 ssh 协议访问,则需要按照以下步骤配置

/usr/local/bin 增加一个文件,名为 git-proxy-wrapper,增加 +x 权限

ruhm@mac:~$ cat /usr/local/bin/git-proxy-wrapper
#! /bin/bash
nc -xlocalhost:1080 -X5 $*

注意,上面需要本机在 1080 端口打开 socks5 代理,端口可以自定义

~/.ssh/config 配置文件中内容如下

Host github.com
    hostname github.com
    User xxxxx
    IdentityFile ~/.ssh/xxxxxx
    ProxyCommand /usr/local/sbin/git-proxy-wrapper '%h %p'

优点

  • ssh 认证不需要提供用户名和密码

缺点

  • 需要一个 socks5 代理

refs

相关推荐