. Advertisement .
. Advertisement .
The Git fatal: Could not read from remote repository error occurs a lot when people mindlessly copy Git commands from the Internet. Keep reading to find the culprit for this problem.
Git fatal: Could not read from remote repository.
Explain The Error
Most of the time, this problem happens when a user accesses a Git repository with the SSH protocol. If you use this command in a system without any SSH key added, the clone operation will fail.
$ git clone [email protected]:github/platform-samples.git Cloning into 'platform-samples'... [email protected]: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights
As the error message tells us, you have no permission to access the remote repository through SSH.
Remember that this protocol is one of the two options popular Git servers like Github implement to handle their traffic (the other protocol is HTTPS).
Their job is to secure the connection between the server and developers. By using underlying encryption keys, SSH and HTTPS can both help you avoid man-in-the-middle (MITM) attacks.
However, you must generate an SSH key, and in the case of Github, add it to your account before performing any operation with the remote repository.
It removes the hassle of entering your username and access token every time you make a connection to the Git server. The lack of this key will result in the error above.
Fix The Error By Configuring SSH Keys
If you wish to use the SSH protocol, you should generate a new key first before adding it to the ssh-agent of your system. You can skip this step if you already have an SSH key.
1. Open the command line on your system and enter:
$ ssh-keygen -t ed25519 -C "[email protected]"
Replace “[email protected]” with your real email address. If your system isn’t up-to-date enough to support the EdDSA signature scheme, or if your Git server doesn’t support it, you can switch to RSA.
$ ssh-keygen -t rsa -b 4096 -C "[email protected]"
Follow the instructions on the screen. Remember the passphrase you have entered.
2. Start ssh-agent with command:
$ eval "$(ssh-agent -s)"
Add your SSH key to ssh-agent:
$ ssh-add ~/.ssh/<ssh_key>
Replace <ssh_key> with the actual public key you have created in the previous step. If you run into the error “Could Not Open A Connection to Your Authentication Agent”, check out this guide.
3. Go to the settings of your Git server (such as Github), and add the SSH key to your account. Consult the service’s documentation for more details.
You can now authenticate successfully with the Git server to clone or pull a remote repository.
$ git clone [email protected]:github/platform-samples.git Cloning into 'platform-samples'... Authenticity of host' github.com (22.214.171.124)' can't be established. Are you sure you want to continue connecting (y/n/[fingerprint])? y Resolving deltas: 100% (1357/1357), done. GitHub SSH clone successful.
Using HTTPS URLs
While SSH is widely considered more secure, you can opt for HTTPS authentication as well. This is a much simpler method as you don’t need to set up anything for tasks like cloning.
Switch to the HTTPS URL and you can now clone the remote repository without any problem:
$ git clone https://github.com/github/platform-samples.git Cloning into 'platform-samples'... remote: Enumerating objects: 2901, done. remote: Total 2901 (delta 0), reused 0 (delta 0), pack-reused 2901 Receiving objects: 100% (2901/2901), 7.29 MiB | 4.93 MiB/s, done. Resolving deltas: 100% (1357/1357), done.
Failure to authenticate your SSH agent is the main reason behind the Git fatal: Could not read from remote repository error. Adding an SSH key or switching to HTTPS can easily resolve the issue.
Leave a comment