场景描述
- windows下,使用git for windows做git仓库的管理,并且使用了Git-Credential-Manager(GCM)进行凭证管理。即manager-core方式
sxp@LAPTOP-MVPFG1UC MINGW64 /d/golang-code/lwjtest/regr (main)
$ git config --get credential.helper
manager-core
sxp@LAPTOP-MVPFG1UC MINGW64 /d/golang-code/lwjtest/regr (main)
$
- 需要管理多个仓库,并且每一个仓库都使用不同的github账号进行push,也就是说每一个仓库需要使用不同的Person Access Token(PAT)进行push。
- 第一个仓库进行push的时候会弹出提示框引导输入PAT,能正常push,但是其他的仓库则push失败,权限存在问题,提示如下:
sxp@LAPTOP-MVPFG1UC MINGW64 /d/golang-code/lwjtest/regr (main)
$ git push origin main
remote: Permission to im1q/regr.git denied to Go-Go-Farther.
fatal: unable to access 'https://github.com/im1q/regr.git/': The requested URL returned error: 403
sxp@LAPTOP-MVPFG1UC MINGW64 /d/golang-code/lwjtest/regr (main)
$
针对上面存在的问题,怎么解决,能同时让每个仓库用自己的PAT正常的push?
原因分析
- 因为git for windows较新的版本使用GCM进行凭证管理,该管理方式一个URI只能关联设置一个账号,导致只能有一个仓库能正常提交push。
- 当windows凭证管理器中有凭证的情况下,git push的时候不在弹窗引导输入账号信息,而使用凭证管理器中存在的那个账号,从而导致其他的仓库push出错。
一种解决方法
- 针对全局的配置一个常用的github账号,关联到https://github.com
- 针对单个仓库局部设置credential.useHttpPath为true,使得URI中的path也作为账号查找的关键部分。使用如下指令:
sxp@LAPTOP-MVPFG1UC MINGW64 /d/golang-code/lwjtest/regr (main)
$ git config --local credential.useHttpPath true
sxp@LAPTOP-MVPFG1UC MINGW64 /d/golang-code/lwjtest/regr (main)
$
- 设置完之后,git push则会弹窗引导输入PAT,输入这个仓库对应的PAT,则能成功push。并且凭证管理器中保存了新的github账号,并关联到https://github.com/new_user/new_repo.git。这样就保证了没有局部设置path用于账号查找的仓库使用全局的github账号。而局部设置path用于账号查找的仓库,则使用完整的URI进行账号查找,保证了不同github账号的所有仓库都可以正常的push。