You can authenticate yourself with a Git repository using either a Secure Shell (SSH) key or a password. If you use an SSH key and fail to configure that key correctly, you’ll encounter the “Permission denied (publickey). fatal: Could not read from remote repository” error.
In this guide, we discuss what this error means. We also discuss why you may encounter it and walk through an example so you can figure out how to solve this error.
Permission denied (publickey). fatal: Could not read from remote repository
SSH keys let you authenticate with a Git repository without worrying about passwords. SSH is the same method of authentication that Linux servers use to allow remote access.
SSH keys rely on public-private key authentication. For this method of authentication to work, your Git server must be configured with a public key, and your local machine must have the corresponding private key.
Git clients like Atlassian and GitHub require that you upload your public key to their dashboards before you can use SSH authentication.
An Example Scenario
We’ve configured a local repository called ck-git. This repository contains one file: README.md.
We are going to link this repository to one on GitHub. To do this, we can use the git remote command:
git remote add origin git@github.com:career-karma-tutorials/ck-git.git
We have created a remote called “origin” to which we can push our code. To push our local repository to GitHub, we can use the git push command:
git push -u origin master
This command uploads our changes to the master branch on our “origin” remote server. Let’s see what happens when we run the command:
Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
We have encountered an error.
Solution #1: Check that your key is used
Before we explore any other solutions, we should make sure that our key is being used to make an SSH connection. We can do this by using the ssh-add command:
eval "$(ssh-agent -s)" ssh-add -l -E md5
The first command starts the SSH agent on your computer. The next command lists all of the SSH keys that are configured on your machine.
Now that you have this list of SSH keys, check to see if they match the one you’ve uploaded to GitHub, Bitbucket, or another version control system you use. If at least one of the keys on the list does not match, you need to add one of them to your version control system.
Solution #2: Adding an SSH key
You may encounter this error if you have not yet added an SSH key to your version control account. The way in which you add an SSH key to a Git repository varies depending on the version control system you use.
For GitHub, you can use the following steps:
- Note down your SSH key using the commands we discussed in Solution #1
- Open GitHub, click on your avatar on the top-right corner and click “Settings”
- Click “SSH and GPG keys” in the sidebar
- Add an SSH key to your account
To add an SSH key to your account, you must first have a key. You can generate one using the following commands:
ssh-keygen -t rsa -b 4096 -C "email@email.com" ssh-add -K ~/.ssh-/id_rsa
Substitute “id_rsa” for the name of your key if you changed it when you were prompted to choose a key name from the first command.
Then, run the following command to see your public key:
cat ~/.ssh/id_rsa.pub
This will give you the string you need to upload into your version control system.
Cause #3: Using the wrong method of authentication
We’ve configured our repository to use an SSH URL:
git remote add origin git@github.com:career-karma-tutorials/ck-git.git
Using this URL means we must use SSH authorized key pairs to authenticate with our repository.
This is only possible if we have set up SSH authentication. If you want to configure your repository with HTTP, which lets you use a username and password to authenticate, you must use an HTTP URL:
git remote add origin https://github.com/career-karma-tutorials/ck-git
When we push our code to our remote server, we’ll be asked for our Git username and password. This will give us a chance to authenticate using HTTP instead of SSH.
Conclusion
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. If you do not have a public key and want to use one to authenticate with Git, you’ll need to create one.
You may want to opt to authenticate using HTTP if you do not want to use SSH. You can do this by using a HTTP URL as the remote URL for your repository.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot