使用-push-提交到远程仓库出现-The-requested-URL-returned-error-403-错误

问题描述

曾用过一个 github 账号进行项目提交,现在使用另一个帐号在同一台机器进行提交时出现错误:

1
2
remote: Permission to userName/repositorieName.git denied to OldUserName.
fatal: unable to access 'https://github.com/userName/repositorieName.git/': The requested URL returned error: 403

问题原因

使用第一个账号提交时,系统保存了该账号的用户信息。在使用新帐号提交时,与已保存的用户信息不一致,所以报错。

win10 解决方案

  • 打开 cmd,输入命令:rundll32.exe keymgr.dll,KRShowKeyMgr,出现「存储的用户名和密码」窗口;
  • 将 github 相关的条目删除;
  • 重新执行提交命令,按提示输入账户名及密码后,即可提交成功。

macOS 解决方案

  • 进入钥匙串,在「登录」下找到「github.com」条目并删除;
  • 重新执行提交命令,按提示输入账户名及密码后,即可提交成功。

通用解决方案

进入库目录,找到 .git/config 文件(macOS 可用终端执行 vi .git/config 直接进入修改),参考内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/userName/repositorieName.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "other"]
remote = origin
merge = refs/heads/other

将用户名加入 [remote “origin”] 中的 url,最终修改为 url = https://userName@github.com/userName/repositorieName.git,接下来在提交项目时会要求输入密码。此后,系统将保存密码信息,以后这个库的提交将不再要求输入密码,也不会出现 403 错误。

通用终极解决方案

在 clone 项目时就将用户名加入路径,原路径如下:

1
git clone -b other https://github.com/userName/repositoryName.git

添加 userName@,该路径修改为:

1
git clone -b other https://userName@github.com/userName/repositoryName.git

接下来在提交项目时会要求输入密码。此后,系统将保存密码信息,以后这个库的提交将不再要求输入密码,也不会出现 403 错误。