[email protected]: Permission denied (publickey). fatal: Could not read from remote repository

beliz picture beliz · Jan 8, 2020 · Viewed 9.9k times · Source

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 [email protected]:my_name/repo.git
git add .
git commit -m 'commit'
git push -u origin master

Then I get the following error:

[email protected]: 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.

Answer

VonC picture VonC · Jan 8, 2020

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 [email protected]

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.