Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
macOS 给 Git(Github) 设置代理(HTTP/SSH)

分辨需要设置的代理

一、HTTP 形式

走 HTTP 代理

git config --global http.proxy "http://127.0.0.1:8080"
git config --global https.proxy "http://127.0.0.1:8080"

走 socks5 代理(如 Shadowsocks)

git config --global http.proxy "socks5://127.0.0.1:1080"
git config --global https.proxy "socks5://127.0.0.1:1080"

取消设置

git config --global --unset http.proxy
git config --global --unset https.proxy

二、SSH 形式

修改 ~/.ssh/config 文件(不存在则新建):

# 必须是 github.com
Host github.com
   HostName github.com
   User git
   # 走 HTTP 代理
   # ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport=8080
   # 走 socks5 代理(如 Shadowsocks)
   # ProxyCommand nc -v -x 127.0.0.1:1080 %h %p
@HJianBo

This comment has been minimized.

Copy link

@HJianBo HJianBo commented Jul 25, 2019

Works for me! Thanks

@allinprogram

This comment has been minimized.

Copy link

@allinprogram allinprogram commented Jul 29, 2019

Thanks!

@wg415196363

This comment has been minimized.

Copy link

@wg415196363 wg415196363 commented Nov 9, 2019

👍

@SealinRepo

This comment has been minimized.

Copy link

@SealinRepo SealinRepo commented Jan 22, 2020

真棒🐂🍺

@banyudu

This comment has been minimized.

Copy link

@banyudu banyudu commented Feb 10, 2020

对于Windows用户,要使用socks5代理却没有 nc 的,可以将
ProxyCommand nc -v -x 127.0.0.1:1080 %h %p
换成
ProxyCommand connect -S 127.0.0.1:1080 %h %p

@laoshufeifei

This comment has been minimized.

Copy link

@laoshufeifei laoshufeifei commented Feb 11, 2020

@banyudu 🐂🍺

@zyd

This comment has been minimized.

Copy link

@zyd zyd commented Feb 13, 2020

对于Windows用户,要使用socks5代理却没有 nc 的,可以将
ProxyCommand nc -v -x 127.0.0.1:1080 %h %p
换成
ProxyCommand connect -S 127.0.0.1:1080 %h %p

亲测好用👍

@devin-wei

This comment has been minimized.

Copy link

@devin-wei devin-wei commented Mar 4, 2020

works for me! 6666666

@bnshen

This comment has been minimized.

Copy link

@bnshen bnshen commented Mar 18, 2020

好用

@terry-xiaoyu

This comment has been minimized.

Copy link

@terry-xiaoyu terry-xiaoyu commented Mar 30, 2020

Have been looking for this for long time, thanks!

@SealinRepo

This comment has been minimized.

Copy link

@SealinRepo SealinRepo commented Apr 2, 2020

SSH方式加了代理配置后github验证失败了, 关掉这个配置是可以正常使用github的,有人遇到过吗?

@samar1tan

This comment has been minimized.

Copy link

@samar1tan samar1tan commented Apr 3, 2020

SSH方式加了代理配置后github验证失败了, 关掉这个配置是可以正常使用github的,有人遇到过吗?

+1

@Unalian

This comment has been minimized.

Copy link

@Unalian Unalian commented Apr 27, 2020

SSH方式加了代理配置后github验证失败了, 关掉这个配置是可以正常使用github的,有人遇到过吗?

一样

@monsterhxw

This comment has been minimized.

Copy link

@monsterhxw monsterhxw commented May 15, 2020

thanks!

@yhwhu

This comment has been minimized.

Copy link

@yhwhu yhwhu commented May 30, 2020

thanks ! http

@youngcong

This comment has been minimized.

Copy link

@youngcong youngcong commented Jun 14, 2020

可真棒👍

@yinxianwei

This comment has been minimized.

Copy link

@yinxianwei yinxianwei commented Jul 14, 2020

thanks

@kallydev

This comment has been minimized.

Copy link

@kallydev kallydev commented Aug 2, 2020

thanks!

@bycgxss

This comment has been minimized.

Copy link

@bycgxss bycgxss commented Aug 10, 2020

SSH方式加了代理配置后github验证失败了, 关掉这个配置是可以正常使用github的,有人遇到过吗?

俺也一样

@nomedia

This comment has been minimized.

Copy link

@nomedia nomedia commented Aug 28, 2020

works thanks

@Xib1uvXi

This comment has been minimized.

Copy link

@Xib1uvXi Xib1uvXi commented Sep 14, 2020

brew install socat 需要安装这个

@mixbe

This comment has been minimized.

Copy link

@mixbe mixbe commented Sep 15, 2020

mac works, thanks

@oldthreefeng

This comment has been minimized.

Copy link

@oldthreefeng oldthreefeng commented Sep 25, 2020

mac works, thanks~

@cnyyk

This comment has been minimized.

Copy link

@cnyyk cnyyk commented Nov 9, 2020

SSH方式加了代理配置后github验证失败了, 关掉这个配置是可以正常使用github的,有人遇到过吗?

遇到这个问题的,都是配置不正确。
1、检查端口。
2、检查依赖命令是否存在

Host *
  ServerAliveInterval 120
  ServerAliveCountMax 5

# macOS 给 Git(Github) 设置代理(HTTP/SSH)
# https://gist.github.com/chuyik/02d0d37a49edc162546441092efae6a1
# 0x00 克隆 repo 的两种方式:https 和 ssh 方式
# https 方式:git clone https://github.com/owner/git.git
# ssh   方式:git clone     git@github.com:owner/git.git

# 0x01 https 方式克隆的 repo,走 http 或 sock5 代理,任选一个
# 0x0101 http 代理
# git config --global http.proxy "http://127.0.0.1:1087"
# git config --global https.proxy "http://127.0.0.1:1087"
# 0x0102 sock5 代理
# git config --global http.proxy "socks5://127.0.0.1:1086"
# git config --global https.proxy "socks5://127.0.0.1:1086"
# 0x0103 取消使用代理
# git config --global --unset http.proxy
# git config --global --unset https.proxy

# 0x02 ssh 克隆方式的代理设置,直接在全局设置文件配置,即 ~/.ssh/config 文件
Host github.com
   HostName github.com
   User git
   # 走 HTTP 代理,需要 brew install socat
   # ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport=1087
   # 走 socks5 代理(如 Shadowsocks)
   ProxyCommand nc -v -x 127.0.0.1:1086 %h %p
   # 走 socks5 代理(如 Shadowsocks),Windows 平台没有 nc 命令
   # ProxyCommand connect -S 127.0.0.1:1086 %h %p
@wangzeping722

This comment has been minimized.

Copy link

@wangzeping722 wangzeping722 commented Nov 20, 2020

好用。

@luchenqun

This comment has been minimized.

Copy link

@luchenqun luchenqun commented Nov 30, 2020

windows的 ssh 亲测有效

ProxyCommand connect -S 127.0.0.1:1080 -a none %h %p

Host github.com
  User git
  Port 22
  Hostname github.com
  # 注意修改路径为你的路径
  IdentityFile "C:\Users\luche\.ssh\id_rsa"
  TCPKeepAlive yes

Host ssh.github.com
  User git
  Port 443
  Hostname ssh.github.com
  # 注意修改路径为你的路径
  IdentityFile "C:\Users\luche\.ssh\id_rsa"
  TCPKeepAlive yes
@forliuyifei

This comment has been minimized.

Copy link

@forliuyifei forliuyifei commented Dec 30, 2020

windows的 ssh 亲测有效

ProxyCommand connect -S 127.0.0.1:1080 -a none %h %p

Host github.com
  User git
  Port 22
  Hostname github.com
  # 注意修改路径为你的路径
  IdentityFile "C:\Users\luche\.ssh\id_rsa"
  TCPKeepAlive yes

Host ssh.github.com
  User git
  Port 443
  Hostname ssh.github.com
  # 注意修改路径为你的路径
  IdentityFile "C:\Users\luche\.ssh\id_rsa"
  TCPKeepAlive yes

感谢, 有效, 用的 clash

# github
Host *github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa
  ProxyCommand connect -S 127.0.0.1:7890 -a none %h %p
@charlygame

This comment has been minimized.

Copy link

@charlygame charlygame commented Jan 10, 2021

Thanks It's work for me.

@LukeMi

This comment has been minimized.

Copy link

@LukeMi LukeMi commented Jan 16, 2021

well done

@coder-free

This comment has been minimized.

Copy link

@coder-free coder-free commented Feb 7, 2021

如何针对域名设置代理? https 我只想针对 github.com 设置代理,其他域名直连

@wangqinggo

This comment has been minimized.

Copy link

@wangqinggo wangqinggo commented Feb 18, 2021

Thanks,great work.

@mickhan

This comment has been minimized.

Copy link

@mickhan mickhan commented Feb 23, 2021

thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment