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

相关推荐
tokenKe6 小时前
Tencent BrowserSkill :让 AI Agent 借用你的真实浏览器| 已登录浏览器与编码 Agent 之间的本地桥 |SSP
chrome·开源·github
玄芯散人10 小时前
【筑基·058】Git协作工作流:冲突解决和PR流程
git·版本控制·团队协作
今夕资源网11 小时前
把 DeepSeek 网页版变成能操作本地项目的 AI Agent:Cuckoo Code 使用指南 GitHub开源
开源·github·deepseek
徐小夕12 小时前
存量.doc 文档怎么办?JitWord 开源转换库打通旧文档到在线协同全链路
后端·架构·github
GHOME12 小时前
CI/CD 学习笔记:GitHub Actions 项目实战
github
troy12812 小时前
Codex 安全盲区:代码漏洞生成实测
windows·python·ci/cd·pycharm·django·github·fastapi
其实防守也摸鱼13 小时前
常见安全架构中 Shiro 的认证确认机制解析
运维·服务器·数据库·windows·github
寺中人13 小时前
Git 版本控制完全入门指南:从安装到实战,提交 + 分支 + 协作 + 冲突解决全拆解
git·开发工具·版本控制·团队协作·代码管理
m4Rk_14 小时前
【论文阅读】Agent 记忆机制(75):TiMem——用时间记忆树实现长期记忆的层级巩固
论文阅读·人工智能·学习·开源·github
荔枝梅梅15 小时前
新手程序员 SSH 第一课:从 Git 仓库连接失败理解 SSH 协议与密钥配置
git