git设置代理, git clone无法连接github怎么办?MAC SSH如何连接github

1. 临时设置
打开 Git Bash,使用命令临时设定socks代理:

git config --global http.proxy 'socks5://127.0.0.1:10808'
 
git config --global https.proxy 'socks5://127.0.0.1:10809'


或者http代理:

git config --global http.proxy 'http://127.0.0.1:10809'  # 最新的git不能加单引号
 
git config --global https.proxy 'https://127.0.0.1:10809'

取消设置

git config --global --unset http.proxy

git config --global --unset https.proxy

直接在命令中使用

git clone -c http.proxy="127.0.0.1:xxxx" https://github.com/Gump8/xxxx.git
git -c http.proxy="127.0.0.1:xxxx" fetch upstream


2. 永久设置
若想要设置代理永久生效,则可以把它写入 .gitconfig 文件中。

使用 vi 打开 .gitconfig 文件:

vi ~/.gitconfig


写入下列配置(建议在最末写入):

[http]
proxy = socks5://127.0.0.1:socks5端口号
proxy = http://127.0.0.1:http端口号
 
[https]
proxy = socks5://127.0.0.1:socks5端口号
proxy = https://127.0.0.1:http端口号


修改后重启 git ,使配置生效:

git config -l --global


查看当前代理:

git config -l


可以看到自己所配置的端口信息,则说明配置成功。

 

3. MAC下SSH设置 

  1. https访问
    仅为github.com设置socks5代理(推荐这种方式, 公司内网就不用设代理了, 多此一举):
    git config --global http.https://github.com.proxy socks5://127.0.0.1:1086
    其中1086是socks5的监听端口, 这个可以配置的, 每个人不同, 在macOS上一般为1086.
    设置完成后, ~/.gitconfig文件中会增加以下条目:

    [http "https://github.com"]
        proxy = socks5://127.0.0.1:1086
  2. ssh访问
    需要修改~/.ssh/config文件, 没有的话新建一个. 同样仅为github.com设置代理:

    Host github.com
        User git
        ProxyCommand nc -v -x 127.0.0.1:1086 %h %p

    如果是在Windows下, 则需要个性%home%.ssh\config, 其中内容类似于:

    Host github.com
        Hostname ssh.github.com
        User git
        Port 443  # 默认22,端口不支持时加此行
        PreferredAuthentications publickey #可选
        IdentityFile ~/.ssh/id_rsa
        ProxyCommand connect -S 127.0.0.1:7890 %h %p #可选

    这里-S表示使用socks5代理, 如果是http代理则为-H. connect工具git自带, 在\mingw64\bin\下面.

posted @ 2024-07-12 18:28  jopny  阅读(131)  评论(0编辑  收藏  举报