gitlabssh-keysgit-pushitermgit-init

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


I am using macOS Catalina. I already have a repository on GitLab and an SSH-key assigned. Now I want to create another repository from the terminal. I do the following:

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

Then I get this:

Initialized empty Git repository in directory

So far so good.

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

Then I get the following error:

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.

Then I go to the repository I already had and try to push there, everything works so I guess I don't have a problem with SSH-key. I know this is a very common question on the internet but none of the answers solved my problem.


Solution

  • First, you should get "Initialized empty Git repository in directory" only after a git init ., not after a git remote add origin ...

    Second, with GitLab, you can push to create a new project, as illustrated in this MR, starting with GitLab 10.5 (Q1 2018)

    Third, if the error persists, then the key is somehow at fault.
    Test it with:

    ssh -Tv git@gitlab.com
    

    Also

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

    To generate a valid key:

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

    And register your new id_rsa.pub to your GitLab profile.