You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用GIT_TOKEN配置pre-commit私有仓库钩子遇错及解决

问题:pre-commit调用私有仓库钩子时认证失败

场景与尝试操作

我需要通过GIT_TOKEN让pre-commit调用私有仓库中的钩子,在执行pre-commit run --all-files前,先执行了以下Git配置命令,试图将GIT_TOKEN嵌入GitHub的访问URL:

git config --global url."https://${GIT_TOKEN}@github.com/".insteadOf "https://github.com/"
git config --global url."https://${GIT_TOKEN}@github.com/".insteadOf "ssh://git@github.com/"

出现的错误

运行pre-commit命令时触发如下错误:

An unexpected error has occurred: CalledProcessError: command: ('/usr/bin/git', 'fetch', 'origin', '--tags')
return code: 128
stdout: (none)
stderr:
    fatal: could not read Username for 'https://github.com': No such device or address
Check the log at /home/jdh/.cache/pre-commit/pre-commit.log

我的pre-commit配置

.pre-commit-config.yaml中的相关配置:

- repo: https://github.com/JakobHavtorn/private-repository
  rev: v0.1.0
  hooks:
  - id: some-hook

排查验证

  • 确认GIT_TOKEN有效,可正常克隆其他私有仓库
  • 使用try-repo本地运行命令无异常
  • 通过/.ssh/config配置SSH方式访问私有仓库也能正常工作

最终解决方案

问题根源在于:默认的git config命令是覆盖已有规则,而非追加。给配置命令添加--add参数后,即可正确生效:

git config --global --add url."https://${GIT_TOKEN}@github.com/".insteadOf "https://github.com/"
git config --global --add url."https://${GIT_TOKEN}@github.com/".insteadOf "ssh://git@github.com/"

内容的提问来源于stack exchange,提问作者Jakob Drachmann Havtorn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 01:00:02