npm获取yarn在安装依赖时 git://github.com/user/xx.git 无法访问解决方法 -- 使用 insteadOf设置git命令别名

今天在使用一个node项目时突然遇到 一个github的拉取异常,一看协议居然是git://xxx 貌似github早就不用这种格式了, 而是使用的git@github.com:xxx 这种或者https协议,解决方法:

使用insteadof设置git别名 url.<base>.insteadOfxx

bash 复制代码
# 这个命令是git别名的配置命令, 会将insteadof后面的地址替换为 url.后面的地址
git config --global url.https://github.com/.insteadOf git://github.com/

上面的命令执行后会在当前用户目录下面的 .gitconfig 文件中自动添加一下2行配置

bash 复制代码
[url "https://github.com/"]
	insteadOf = git://github.com/

这样,当我们在使用 git://github.com/ 进行克隆和拉取远程仓库时,Git会自动将其转换为 https://github.com/ 这样就成功解决了这个git://无法拉取的问题。

当然这个别名设置命令也可以解决类似git无法访问或者访问协议相关的问题。

这个别名设置命令的官方解释:

url.<base>.insteadOf

Any URL that starts with this value will be rewritten to start, instead, with <base>. In cases where some site serves a large number of repositories, and serves them with multiple access methods, and some users need to use different access methods, this feature allows people to specify any of the equivalent URLs and have Git automatically rewrite the URL to the best alternative for the particular user, even for a never-before-seen repository on the site. When more than one insteadOf strings match a given URL, the longest match is used.

Note that any protocol restrictions will be applied to the rewritten URL. If the rewrite changes the URL to use a custom protocol or remote helper, you may need to adjust the protocol.*.allow config to permit the request. In particular, protocols you expect to use for submodules must be set to always rather than the default of user. See the description of protocol.allow above.

参考文档 Git - git-config Documentation

相关推荐
玖玖passion3 小时前
Windows 上部署 Hermes Agent 完整指南 - 让你的 AI 助手在 WSL2 中跑起来
前端·后端·github
AC赳赳老秦5 小时前
OpenClaw实战案例:用Agent实现每日工作日报自动生成+发送
人工智能·python·职场和发展·eclipse·github·deepseek·openclaw
Hommy886 小时前
【开源剪映小助手】调试与故障排除
开源·github·aigc
摆烂z7 小时前
AI同时完成多个功能(Git WorkTree)
git
CoderJia程序员甲8 小时前
GitHub 热榜项目 - 日榜(2026-04-24)
人工智能·ai·大模型·github·ai教程
___波子 Pro Max.11 小时前
Git Worktree 可视化理解指南
git
AI技术增长12 小时前
第 2 集:环境搭建:安装 Claude Code、GitHub CLI 与项目初始化
人工智能·自动化·github
happymaker062613 小时前
git使用快速入门
git
MuzySuntree13 小时前
Ubuntu 使用 GitHub SSH 克隆时报 Permission denied (publickey) 解决方案
ubuntu·ssh·github
不做超级小白14 小时前
从零到可用:在手机上用 Termux + Git + Obsidian 打造稳定同步环境(踩坑全记录)
git·智能手机