简介
在工作中,有同时需要同时拉取多个仓库项目的需求 例如,github,gitlab. 拉取git仓库代码的方式主要有一下几种:
- ssh git@github.com/panjf2000/ants.git
- password github.com/panjf2000/a…
- token oauth2:accesstoken@xxx.gitlab.com 其中,http或者https需要看搭建的仓库的支持,例如,github必须采用https,而私有仓库在内网往往采用http
ssh方式
ssh多账号管理已经介绍过如何在同一台机器上部署多个git账号
password方式
git的全局配置文件中,增加如下配置,即可使用https方式拉取多个账号,http协议和https方式配置相同,只是替换协议
[credential]
helper = store
[credential "https://github.com"]
username = $GITHUB_USERNAME
password = $GITHUB_PASSWORD
[credential "https://yourgitlab.com"]
username = $GITLAB_USERNAME
password = $GITLAB_PASSWORD
token方式
- 生成token,各种git仓库生成的方式不同,具体的方式可以查看响应的文档. 以gitlab为例,假定生成的token为
aabb-ynSHYzBizbeC_3jV6XjC - 配置git
// 仓库采用https
git config --global url."https://oauth2:aabb-ynSHYzBizbeC_3jV6XjC@gitlab.yourdns.com".insteadof "https://yourgitlab.com"
// 仓库采用http
git config --global url."http://oauth2:aabb-ynSHYzBizbeC_3jV6XjC@yourgitlab.com".insteadof "https://yourgitlab.com"
go 配置私有仓库
由于go mod tidy 默认采用的方式是https, 假设私有仓库采用ssh或者http方式,需要如下配置:
- git配置
// git仓库https传输,采用token模式鉴权
git config --global url."https://oauth2:aabb-ynSHYzBizbeC_3jV6XjC@yourgitlab.com".insteadof "https://yourgitlab.com"
// git仓库http传输,采用token模式鉴权
git config --global url."http://oauth2:aabb-ynSHYzBizbeC_3jV6XjC@yourgitlab.com".insteadof "https://yourgitlab.com"
// git仓库https传输,仓库采用用户名密码鉴权,全局配置文件增加如下形式
[credential "https://yourgitlab.com"]
username = $GITLAB_USERNAME
password = $GITLAB_PASSWORD
// git仓库http传输,仓库采用用户名密码鉴权,全局配置文件增加如下形式
[credential "http://yourgitlab.com"]
username = $GITLAB_USERNAME
password = $GITLAB_PASSWORD
// 仓库采用git
git config --global url."git@/yourgitlab.com".insteadof "https://yourgitlab.com"
- go 环境配置
// 仓库采取http协议拉取:
go env -w GOINSECURE=yourgitlab.com
// 仓库配置以git协议拉取:
go env -w GOPRIVATE=yourgitlab.com
总结
使用token来拉取git仓库代码是比较良好的方式, go mod tidy 拉取go package时,默认是https协议,此时,如果私有仓库不支持https,需要在git中配置,替换仓库的url,同时,增加私有仓库的鉴权