如何解决远程SSH推送Github错误

3
我创建了与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: 拒绝访问(publickey)。 fatal: 无法从远程仓库读取。
请确保您拥有正确的访问权限并且仓库存在。
我已经检查了ssh密钥是否已链接,而且已经链接了。甚至还提示了。
git remote -v

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

我尝试了几乎所有的答案,但都没有起作用。请有人帮助我。


试试这个,https://gist.github.com/jexchan/2351996#modify-the-ssh-config。 还有删除.ssh/known_hosts文件。 - undefined
请详细解释一下你具体尝试了什么? - undefined
2个回答

2

您不应该收到此错误,因为您已经设置了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

1
你已经创建了 SSH 密钥并将公钥链接到 GitHub 帐户,这是需要先完成的步骤。
然后,您需要将 RSA 密钥添加到 ssh-agent,并执行身份验证命令以测试您的密钥是否已与 GitHub 同步。可以通过以下命令完成这些操作(在将公钥添加到 GitHub 后)。

eval "$(ssh-agent -s)"  // evaluate process ID
ssh-add ~/.ssh/<required key> //add key to agent 
ssh -T git@github.com 

after these you'll get an output like this .. "Hi <your_user_name>! You've successfully authenticated, but GitHub does not provide shell access."


我尝试了,但在第二个命令中,它无法识别目录中的SSH密钥。 - undefined
在第二个命令中,您应该添加私钥,而不是公钥(即<name>.pub)。 - undefined

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