向GitLab推送代码时被要求输入GitHub凭证问题求助
问题描述
我在GitLab GUI上创建了一个项目,现在想往这个已有项目推送文件。执行git remote add origin https://git.doit.wisc.edu/my_user_name/my-website.git时,收到错误提示:error: remote origin already exists.,于是我跳过这步,执行git branch -M main,输入钥匙串密码后无异常。但执行git push -uf origin main时,终端提示输入Username for 'https://github.com':,输入用户名后收到错误:
remote: Support for password authentication was removed on August 13, 2021. remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
实际我要推送到GitLab而非GitHub,请问操作中遗漏了什么?
原因
你的本地仓库里origin这个远程仓库地址早就被设置成GitHub的了,跳过添加远程的步骤后,Git依然会使用旧的GitHub地址进行推送,所以才出现了混淆。
解决方法
方法一:直接更新现有远程地址
先确认当前
origin的地址,执行:git remote -v
你会看到输出里origin对应的是GitHub的地址。将
origin的地址替换为你的GitLab项目地址:git remote set-url origin https://git.doit.wisc.edu/my_user_name/my-website.git重新执行推送命令:
git push -uf origin main
这时候就会指向你的GitLab仓库,按提示完成认证即可。
方法二:删除旧远程后重新添加
删除现有的GitHub远程地址:
git remote remove origin添加你的GitLab项目作为新的
origin远程:git remote add origin https://git.doit.wisc.edu/my_user_name/my-website.git执行推送命令:
git push -uf origin main
内容的提问来源于stack exchange,提问作者DiamondJoe12

