Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 764
beliz Avatar asked Jan 08 '20 21:01

beliz


People also ask

How do I fix permission denied Publickey fatal could not read from remote repository please make sure you have the correct access rights and the repository exists?

The “Permission denied (publickey). fatal: Could not read from remote repository” error is caused by an issue with the way in which you authenticate with a Git repository. To solve this error, make sure your key is being used on your Git account. If it is not, add your key to Git.

Could not read from remote repository in GitLab?

The Git “fatal: Could not read from remote repository” error occurs when there is an issue authenticating with a Git repository. This is common if you have incorrectly set up SSH authentication. To solve this error, make sure your SSH key is in your keychain and you connecting to a repository using the correct URL.

How do I fix permission denied Publickey password?

Solution 1: Enable Password Authentication If you want to use a password to access the SSH server, a solution for fixing the Permission denied error is to enable password login in the sshd_config file. In the file, find the PasswordAuthentication line and make sure it ends with yes .

Why do I get permission denied Publickey?

"Permission denied (publickey)" and "Authentication failed, permission denied" errors occur if: You're trying to connect using the wrong user name for your AMI. The file permissions within the operating system are incorrect on the instance. The incorrect SSH public key (.


2 Answers

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.

like image 148
VonC Avatar answered Sep 20 '22 08:09

VonC


I tried all the above mentioned solutions but none of it worked. I then read the logs and found that it is looking for the key in a specific folder and I created the key and added it to my Gitlab profile too. Then it started working.

Git authentication issue can be solved by reading the logs of the git and creating appropriate SSH keys under appropriate folders.

Steps
  1. Run the following command and it will try to push the code and if it not successful then it will display where the error is

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

The error while trying to push the code

  1. Now, we can generate a new SSH key and the following command will generate a key in the working folder.

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

It will ask for key name, you can give id_rsa as the key name or any name which the Bash displays as "Trying private key: c:/Users/Dell/.ssh/". Once the key is generated in bash, your working directory will have the key.

The SSH Key on working directory

  1. While running the command in step1, you will see that the folder in which it is looking for a private key. In my case it is "c:/Users/Dell/.ssh/id_rsa"

The output of Git Bash

  1. We should put the generated keys from the working folder into this folder

The folder path in which image has to be copied

  1. We should also make sure that we add our SSH Key to the Gitlab account. Click on your Gitlab account MyProfile and select preferences. Click to see how to add SSH to your Gitlab account  

  2. Click the SSH keys menu, open the generated key file using notepad and copy the content of the key from notepad and paste it in the SSH key text editor and save it . Click to see how to add SSH Key to your Gitlab account

  3. Again, run the following command and check now. The code will be pushed.

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

the code will be pushed.

the new key is read and pushed in Git

like image 42
venkat Avatar answered Sep 19 '22 08:09

venkat