beliz
beliz

Reputation: 402

[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.

Upvotes: 13

Views: 49941

Answers (4)

Cryptoer17
Cryptoer17

Reputation: 111

< Git looks at ssh keys by name, picking the ones starting with id_ first... In case there are two ssh keys with names starting with id_ it tries both and see if one of them is fine. If your ssh key name starts with a different name you need to change it.

Example:

keyssh (private key) change the name to--> id_rsa (private key)
keyssh.pub (public key) change the name to--> id_rsa.pup (public key)

< I also created a file named "config" in the .ssh folder.
The "config" file has no extension and is needed for correct configuration/management of the ssh key:

enter image description here

< Also, I added the login credentials and the network domain in the windows credential manager:

Internet or network address: https://corporate-gitlab.xxxxx.com
Username: [email protected]
Password: ••••••••••

This way I solved the 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.

Upvotes: 2

Raikumar Khangembam
Raikumar Khangembam

Reputation: 1008

The same issue happened. I used HTTPS instead of SSH

(I followed the instruction steps after creating repo in GitLab but that cause a Permission issue. It's is because of ssh pub key to upload)

These steps work without using SSH

  1. Create a repository/project in GitLab

  2. I removed .git (that caused permission issue in previous. For to start with fresh)

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

    git config --global user.email "[email protected]"

  4. git init .

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

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

  7. git push -u origin master

It will ask username and password. Then fixed.

Upvotes: 4

venkat
venkat

Reputation: 51

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

Upvotes: 5

VonC
VonC

Reputation: 1324278

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.

Upvotes: 12

Related Questions