2025年3月29日 星期六 甲辰(龙)年 月廿八 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 软件应用 > 网络应用

使用本地代理给 Git 和 NPM 加速

时间:10-23来源:作者:点击数:68

由于中国大陆特殊的网络环境,Git ( 此处一般指 Git 连接 GitHub )、NPM 包管理器在国内使用起来可能不是那么容易,可能偶尔或经常的出现抽风的情况。虽然大家都使用着各种本地代理工具,但是诸如 Git 这些默认是不走代理的(即使全局代理也不可以),本篇记录给它们配置本地代理以获得更佳体验

查看端口

常用的代理软件一般都很容易看到本地 http 代理和 socks5 代理端口,此处以 Windows 下最好用的 Clash For Windows 为例,其他代理软件都同理啦~( 以下简称 CFW )

查看 CFW 端口

老版本的 CFW 使用的 http 端口和 socks5 端口是不同的,但是在新版本中开始使用 Mixed Port ( 混合端口 ),因此此处都是本地端口 7890 ,一切以实际为准

配置Git代理

Git 同时支持 http 代理和 socks5 代理,二选一即可

  • # http代理
  • $ git config --global http.proxy http://server:port
  • $ git config --global https.proxy http://server:port
  • # socks5代理
  • $ git config --global http.proxy socks5://127.0.0.1:7891
  • $ git config --global https.proxy socks5://127.0.0.1:7891

以 CFW 代理工具为例:

  • # http代理
  • $ git config --global http.proxy http://127.0.0.1:7890
  • $ git config --global https.proxy http://127.0.0.1:7890
  • # socks5代理
  • $ git config --global http.proxy socks5://127.0.0.1:7890
  • $ git config --global https.proxy socks5://127.0.0.1:7890

取消 Git 代理:

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

配置NPM代理

NPM 原生支持 http 代理类型,但是不支持 socks5 代理类型,如果还想要使用 socks5 代理,可能还需要使用一个工具使用 http 监听 socks5 代理 (禁止套娃),此处不做讨论

  • # http代理
  • $ npm config set proxy http://server:port
  • $ npm config set https-proxy http://server:port

以 CFW 代理工具为例:

  • $ npm config set proxy http://127.0.0.1:7890
  • $ npm config set https-proxy http://127.0.0.1:7890

取消 NPM 代理:

  • $ npm config delete proxy
  • $ npm config delete https-proxy

配置Yarn代理

Yarn 原生就拥有比 NPM 更快的速度,类似 NPM,使用 http 代理(必要的话),打开 Terminal/Bash:

  • # http代理
  • $ yarn config set proxy http://server:port
  • $ yarn confit set https-proxy http://server:port

以 CFW 代理工具为例:

  • $ yarn config set proxy http://127.0.0.1:7890
  • $ yarn confit set https-proxy http://127.0.0.1:7890

取消 Yarn 代理:

  • $ yarn config delete proxy
  • $ yarn config delete https-proxy
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门