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

今天在使用一个node项目时突然遇到 一个github的拉取异常,一看协议居然是git://xxx 貌似github早就不用这种格式了, 而是使用的[email protected]: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 小时前
解决Windows系统下Git克隆时报错“unable to checkout working tree”的方法详解
windows·git
island13141 小时前
【git#4】分支管理 -- 知识补充
大数据·git·elasticsearch
qianmoQ2 小时前
GitHub 趋势日报 (2025年04月24日)
github
船长@Quant3 小时前
协作开发攻略:Git全面使用指南 — 引言
git·版本控制·源代码管理·协作开发
极小狐3 小时前
极狐GitLab 项目功能和权限解读
运维·git·安全·gitlab·极狐gitlab
极小狐3 小时前
极狐GitLab 如何 cherry-pick 变更?
人工智能·git·机器学习·gitlab
DN金猿4 小时前
使用npm install或cnpm install报错解决
前端·npm·node.js
muzidigbig4 小时前
开发并发布一个属于自己的包(npm)
npm·开发npm包步骤·npm包使用es6模块导入
前端太佬6 小时前
从拧螺丝到造火箭:Git高阶玩家生存报告
前端·git·github
前端太佬6 小时前
从青铜到塑料:Git逃难指南(附救命指令大全)
前端·git·github