问题:gitlab开启了双因素认证
如进行了 OAuth configuration
在进行git操作时如下提示
remote: HTTP Basic: Access denied. The provided password or token is incorrect or your account has 2FA enabled and you must use a personal access token instead of a password.
解决办法
1、生成 Personal access tokens
Scopes部分选择write_repository即可,或者Scopes都选上,其他按需配置,复制Token,这个只显示一次
2、使用 Personal access tokens
命令行进行git
操作,如
直接使用access token
拉取代码:
git clone https://oauth2:生成的Access-TOKEN@gitlab.z_johnny.com/test/xxx.git
每次拉取代码都需要加上access token
稍微有点麻烦,可以配置一下:
git config --global url."https://oauth2:生成的Access-TOKEN@gitlab.z_johnny.com".insteadof "https://gitlab.z_johnny.com"
配置好后,每次按正常的拉取方式拉取代码就好了:
git clone https://gitlab.z_johnny.com/test/xxx.git
常见问题
Windows配置了token拉取失败
Windows在c盘下的用户目录下的windows账户下,有一个gitconfig的隐藏文件,添加如下图片中的两行命令,里面的oauth2:后面就是token,替换为自己的token即可。比如"C:\Users\johnny\.gitconfig"
MacOS & Linux 配置了token拉取失败
MacOS找到用户目录下的“.gitconfig”隐藏文件 添加如下两行代码,里面的token换成个人自己的token即可。
[url "https://oauth2:生成的Access-TOKEN@gitlab.z_johnny.com"]insteadof = gitlab.z_johnny.com