git push推送到了错误的仓库

10

最近我在网上学习编程,课程要求我们将作业上传到Github。从那时起,每当我输入git push -u origin master时,就会出现以下提示:

remote: Permission to my-github-username/mygithubrepo.git denied to rheros.
fatal: unable to access 'https://github.com/my-github-username/mygithubrepo.git/': The requested URL returned error: 403
rheros是我之前用来提交作业的仓库名称。现在我不再使用rheros,但命令行仍然认为我要推送到这个仓库。我尝试检查是否有错误的SSH密钥,但一切看起来都很正常。
我该如何让命令行有效地忘记之前提交到完全无关的目录的存在? 编辑 git remote --verbose 的输出如下:
origin  https://github.com/my-github-username/mygithubrepo.git (fetch)
origin  https://github.com/my-github-username/mygithubrepo.git (push)

到目前为止,以下的建议都没有帮助我解决问题:似乎rheros仓库正在我的命令行背景中运行,我无法弄清如何消除它。

你能否添加(并注释)git remote --verbose的结果? - user3276552
@user3276552 完成了,见编辑。 - suka_snake
你的Github用户名和你想要推送的仓库是什么?你在Github上注册了第二个账户吗? - user3276552
是的,好的,这可能是一个问题,我有多个Github账户。 - suka_snake
你用的是Linux、Mac还是Windows操作系统? - user3276552
显示剩余23条评论
6个回答

8

首先使用以下命令检查当前是否存在Git仓库:

git remote -v

然后,通过检查GitHub网站并复制正确的仓库URL,确认您的新存储库存在

运行以下命令以覆盖存储库URL:

git remote add origin https://github.com/my-github-username/mygithubrepo.git

请再次尝试执行git push:

git push origin master

8
如果你收到“fatal: remote origin already exists”错误提示,请使用set-url代替add命令。 - Mageek

3
git remote remove origin https://github.com/my-github-username/mygithubrepo.git

之后,您可以像平常一样添加要推送的存储库


2

如果您想更改当前的源路径,请尝试以下操作:

git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

1

0

git remote add origin https://github.com/my-github-username/mygithubrepo.git

然后再尝试推送


0
这是因为 Github 正在尝试查找错误的存储库(很可能是如此)。解决方案很简单,请按照以下步骤操作:
  1. 使用以下命令检查 origin 是否指向您的 repo 的正确 URL:
    git remote -v

如果不删除所有的原始内容,请使用以下方法:
    git remote rm origin
  1. 前往github-desktop,尝试将其推送到存储库。如果它说存储库名称已存在,请从github.com中删除该存储库。
  2. 再次尝试使用github-desktop进行推送。
  3. 检查您的起点网址。现在应该指向正确的网址。
    git remote -v

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接