Git 在推送代码时返回错误 403

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51231996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:09:24  来源:igfitidea点击:

Git returning Error 403 while pushing the code

gitgithub

提问by calvin.mk

Everything was working fine, until I create a new GitHub account. When I try to push the code for first time to github server with new account, following error is occurring:

一切正常,直到我创建了一个新的 GitHub 帐户。当我第一次尝试使用新帐户将代码推送到 github 服务器时,发生以下错误:

remote: Permission to NEW_USER/NEW_REPO.git denied to OLD_USER.
fatal: unable to access 'https://github.com/NEW_USER/NEW_REPO/': The requested URL returned error: 403

Tried to reset remote url using:

尝试使用以下方法重置远程 url:

$git remote set-url origin https://[email protected]/user/repo.git... but not working.

$git remote set-url origin https://[email protected]/user/repo.git...但不工作。

Got one solution here, but for Mac OS. Any ideas how to resolve this. Using git bash version 2.16.1.windows.4.

这里有一个解决方案,但适用于 Mac OS。任何想法如何解决这个问题。使用git bash version 2.16.1.windows.4.

Thanks

谢谢

回答by codeworks elegant

This happens when two git accounts are being used on same machine. On Windows 7 you need to remove git Credentials from Windows vault.

当在同一台机器上使用两个 git 帐户时会发生这种情况。在 Windows 7 上,您需要从 Windows 保管库中删除 git Credentials。

  • go to Control Panel -> User Accounts -> Manage your credentials.
  • Under Generic Credentials delete entries for github credentials.
  • Try to push code and this time it will prompt you for correct username and password again.

  • 转到控制面板 -> 用户帐户 -> 管理您的凭据。
  • 在通用凭据下删除 github 凭据的条目。
  • 尝试推送代码,这次它会再次提示您输入正确的用户名和密码。

enter image description here

在此处输入图片说明

回答by Geno Chen

You may check for the Git Credentialsyou are using, and change that with your new account.

您可以检查您正在使用的Git 凭证,并使用您的新帐户进行更改。

As the top answer from the question you mentioned says, using a SSH way by

正如您提到的问题的最佳答案所说,使用 SSH 方式

$ git remote set-url origin [email protected]:new_user/new_repo.git

(and set the SSH Key) instead of HTTPS way is also a workaround.

(并设置 SSH 密钥)而不是 HTTPS 方式也是一种解决方法。

回答by Chathura Buddhika

Go to %UserProfile%(Eg: C:\Users\CHATHURA) and delete all .gitconfigfiles.

转到%UserProfile%(例如:C:\Users\CHATHURA)并删除所有.gitconfig文件。

Then set user.name and user.email again

然后再次设置 user.name 和 user.email

$ git config --global user.name "Your User Name"
$ git config --global user.email [email protected]

Go to repository directory and set user.name and user.email for the repository.

转到存储库目录并为存储库设置 user.name 和 user.email。

$ git config user.name "Your User Name"
$ git config user.email [email protected]

Make sure these email and usernames are same as your Github login username and password.

确保这些电子邮件和用户名与您的 Github 登录用户名和密码相同。

The go to your repository and copy repository URL. If you are using HTTPS it should look like,

转到您的存储库并复制存储库 URL。如果您使用的是 HTTPS,它应该看起来像,

https://github.com/user/repo.git

Then change your remote with correct URL

然后使用正确的 URL 更改您的遥控器

git remote rm origin
git remote set-url origin https://github.com/user/repo.git

Then try to push your commits. This should fix your problem.

然后尝试推送您的提交。这应该可以解决您的问题。