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

相关推荐
绝无仅有1 小时前
未来教育行业的 Go 服务开发解决方案与实践
后端·面试·github
逛逛GitHub5 小时前
这 4 个牛逼 GitHub 开源项目,太优质了。
github
知行力6 小时前
【GitHub每日速递】不止 TeamViewer 替代!RustDesk 与 PowerToys,Windows 效率神器
windows·github·teamviewer
杨杨杨大侠7 小时前
实战案例:商品详情页数据聚合服务的技术实现
java·spring·github
杨杨杨大侠7 小时前
实战案例:保险理赔线上审核系统的技术实现
java·spring·github
FutureUniant8 小时前
GitHub每日最火火火项目(9.5)
人工智能·microsoft·计算机视觉·ai·github
CoderJia程序员甲8 小时前
GitHub 热榜项目 - 日榜(2025-09-05)
ai·开源·github·ai编程·github热榜
绝无仅有8 小时前
从拉取代码到前端运行访问:Vue 前端项目的常规启动流程
后端·面试·github
游学者伊奈帆10 小时前
CI/CD 基础与 GitHub Actions 总结
驱动开发·ci/cd·github
Doris_LMS18 小时前
Git的强软硬回退(三)
运维·服务器·数据库·git·idea