使用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
相关产品推荐
相关产品推荐

