如何在 Composer 中使用 config.github-protocols 来选择 git 或 https 协议?

admin 百科 14
Composer 默认优先用 git 协议(SSH)克隆 GitHub 仓库,但在防火墙或 CI 环境中易失败;可通过 config.github-protocols 设为 ["https"] 强制走 HTTPS,支持项目级、全局级和临时命令行三种设置方式,适用于无 SSH 权限等场景。

如何在 Composer 中使用 config.github-protocols 来选择 git 或 https 协议?-第1张图片-佛山资讯网

Composer 默认会优先使用 git 协议克隆 GitHub 仓库(例如 git@github.com:vendor/package.git),但这在某些受限网络环境(如公司防火墙、CI/CD 环境)下可能失败。你可以通过 config.github-protocols 明确指定 Composer 应该用 git 还是 https(或两者)来访问 GitHub。

作用和默认行为

该配置控制 Composer 解析 github.com 域名时使用的协议前缀。默认值是 ["git", "https"],意味着 Composer 会先尝试 git://(实际走 SSH 或 HTTPS 的 git 协议封装),失败再回退到 https://。但如果你明确设为 ["https"],它就只走 HTTPS,绕过 SSH 密钥和 git 端口(9418)限制。

设置方式

有三种常用设置位置,按优先级从高到低:

  • 项目级(推荐):在项目根目录的 composer.json 中添加:

"config": {
  "github-protocols": ["https"]
}

登录后复制

然后运行 composer updatecomposer install 生效。

  • 全局级:对当前用户所有项目生效:

composer config -g github-protocols '["https"]'

登录后复制

  • 临时命令行:仅本次命令生效(适合调试):

composer install --github-protocols=https

登录后复制

常见组合与适用场景

你可以传入一个字符串数组,顺序决定尝试优先级:

标签: composer github协议 js git json github 防火墙 端口 ai 字符串数组

发布评论 0条评论)

还木有评论哦,快来抢沙发吧~