首页 > 解决方案 > 如何解决远程 SSH 推送 github 错误

问题描述

我创建了链接到 github 的 SSH 密钥。我正在尝试将文件远程推送到创建的存储库

这些是我的代码

git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/username/demo_repo2.git
git push -u origin main

但它给了我错误:

git@github.com:权限被拒绝(公钥)。致命:无法从远程存储库中读取。

请确保您具有正确的访问权限并且存储库存在。

我检查了 ssh 密钥是否已链接,确实如此。它甚至给

git remote -v

origin  git@github.com:user/repo2.git (fetch)
origin  git@github.com:user/repo2.git (push)

我尝试了几乎所有的答案,但没有奏效。请有人帮助我

标签: gitgithubsshremote-repository

解决方案


它不应该给你这个错误,因为你已经设置了一个 HTTPS URL:

git remote add origin https://github.com/username/demo_repo2.git

检查git config -l任何url."git@github.com:".insteadOf https://github.com/指令。

如果git remote -v确实给了你一个 SSH URL,那意味着第一个git remote add origin肯定失败了。
您可以强制使用 HTTPS URL:

cd /path/to/my/repo
git remote set-url  origin https://github.com/username/demo_repo2.git

推荐阅读