git@gitlab.com:权限被拒绝(公钥)。严重的:无法从远程存储库读取

我正在使用macOS Catalina。 我已经在GitLab上建立了一个存储库,并分配了SSH-key现在,我想从终端上创建另一个存储库。我执行以下操作:

git config user.name my_name
git config user.email my_email
git init

然后我得到了:

Initialized empty Git repository in directory

到目前为止一切顺利。

git remote add origin git@gitlab.com:my_name/repo.git
git add .
git commit -m 'commit'
git push -u origin master

然后我得到以下错误:

git@gitlab.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

然后,我进入已经拥有的存储库并尝试将其推送到那里,一切正常,因此我想我对SSH-key没问题。我知道这是互联网上一个非常普遍的问题,但是没有一个答案能解决我的问题。

jianjunliqi 回答:git@gitlab.com:权限被拒绝(公钥)。严重的:无法从远程存储库读取

首先,您应该仅在git init .之后而不是在git remote add origin ...之后获得“目录中的初始化的空Git存储库”

第二,从GitLab 10.5(2018年第一季度)开始,您可以以push to create a new project的身份{@ 3}} illustrated in this MR

第三,如果错误仍然存​​在,则说明密钥存在某种错误。
用以下方法进行测试:

ssh -Tv git@gitlab.com

git -c core.sshCommand="ssh -v" push -u origin master

要生成有效密钥:

ssh-keygen -t rsa -P "" -m PEM

并将新的id_rsa.pub注册到GitLab个人资料中。

,

我尝试了上述所有解决方案,但没有一个奏效。然后我阅读了日志,发现它正在特定文件夹中寻找密钥,我创建了密钥并将其添加到我的 Gitlab 配置文件中。然后它开始工作。

Git 身份验证问题可以通过读取 git 的日志并在适当的文件夹下创建适当的 SSH 密钥来解决。

Steps
  1. 运行以下命令,它会尝试推送代码,如果不成功,则会显示错误所在

    git -c core.sshCommand="ssh -v" push -u origin master
    
    

The error while trying to push the code

  1. 现在,我们可以生成一个新的 SSH 密钥,下面的命令将在工作文件夹中生成一个密钥。

    ssh-keygen -t rsa -P "" -m PEM
    
    

它会询问密钥名称,您可以将 id_rsa 作为密钥名称或 Bash 显示为“尝试私钥:c:/Users/Dell/.ssh/”的任何名称。 在 bash 中生成密钥后,您的工作目录将拥有密钥。

The SSH Key on working directory

  1. 在步骤 1 中运行命令时,您将看到它正在寻找私钥的文件夹。就我而言,它是“c:/Users/Dell/.ssh/id_rsa”

The output of Git Bash

  1. 我们应该把工作文件夹中生成的密钥放到这个文件夹中

The folder path in which image has to be copied

  1. 我们还应该确保将我们的 SSH 密钥添加到 Gitlab 帐户。 单击您的 Gitlab 帐户 MyProfile 并选择首选项。 Click to see how to add SSH to your Gitlab account

  2. 点击 SSH 密钥菜单,使用记事本打开生成的密钥文件,从记事本中复制密钥内容,粘贴到 SSH 密钥文本编辑器中并保存。 Click to see how to add SSH Key to your Gitlab account

  3. 再次运行以下命令并立即检查。代码将被推送。

    git -c core.sshCommand="ssh -v" push -u origin master
    
    

代码将被推送。

the new key is read and pushed in Git

,

同样的问题发生了。 我使用了 HTTPS 而不是 SSH

(我在 GitLab 中创建 repo 后按照说明步骤操作,但导致权限问题。这是因为要上传的 ssh pub 密钥)

这些步骤无需使用 SSH

  1. 在 GitLab 中创建存储库/项目

  2. 我删除了 .git(导致之前的权限问题。从新开始)

  3. git config --global user.name "user_name"

    git config --global user.email "user.email@gmail.com"

  4. git init .

  5. git remote add origin https://gitlab.com/user.account/user_project.git

  6. git add .git commit -m "initial commit"

  7. git push -u origin master

它会询问用户名和密码。然后修复。

本文链接:https://www.f2er.com/2801400.html

大家都在问