Table of Contents
This tutorial shows you the two main origins and solutions to the error, "fatal: could not read from remote repository." These are:
- Git pushing to non-existing remote URL
- Failing to config SSH keys properly
First, the tutorial briefly takes you through Git remote and creating an SSH connection between Git and GitHub. Next, the tutorial creates the error then shows you how to solve it step by step.
We use GitHub for the demos, but you can still follow along with a different website.
Git remote
Git tracks its connection to GitHub using the git remote
command. For example, you can check the destination URLs using the git remote -v
command.
steve@thisHostname:~/workingDir/pyFiles/.git$ git remote -v
origin https://github.com/Stevealila/Python.git (fetch)
origin https://github.com/Stevealila/Python.git (push)
origin
is an alias for the https://github.com/Stevealila/Python.git
URL. You can modify or update it.
The https
part shows that the connection is via HTTPs, not SSH. github.com
is the website, while Python
is the name of the remote repository. (fetch)
or (push)
means we can get (clone or pull) or send (push) data through the remote URL, respectively.
Since getting or sending data to a remote may occur through multiple branches, you need to specify the (default is main) branch after the origin
part.
git push origin main
You may ignore the origin main
part if the remote only has one branch and the branch is checked out during the git push
process.
git push
The key takeaway is that Git must know the path to fetching from or pushing changes to a remote repository. Otherwise, it may throw the error fatal: could not read from remote repository.
Before diving into practical examples, let me show you another common origin of the error.
SSH Connection
Secure Shell (SSH) ensures two remote computers can connect and share data securely. In this case, your (local) computer and (remote) GitHub server want to share code.
But instead of asking you to input your username and password every time you want to push to the remote repository, SSH automatically completes the connection because the computers already remember each other after the initial connection.
So, you probably get the message, "fatal: could not read from remote repository." because the remote machine cannot remember (authenticate) your local computer.
But how do you know if an SSH connection exists?
First, check the existence of the keys in the ~/.ssh
folder of your local computer.
ls ~/.ssh
Better yet, you can generate a new one.
ssh-keygen -t ed25519 -C "<your GitHub email address>"
ssh-keygen
is the command to generate a key.
The -t
option specifies the type of encryption used to generate the key. I have used ed25519
algorithm. You can also try out rsa
algorithm.
The -C
option stands for comment.
Running the command outputs an SSH key pair: public and private. The public key ends in a .pub
extension, while the private key lacks an extension.
You should inform your computer's SSH agent about the new keys. The agent is the wallet that safely stores your various SSH keys. You can start up the SSH agent using the following commands.
eval "$(ssh-agent -s)"
Next, store your private key in the agent's wallet using the following commands.
ssh-add ~/.ssh/id_ed25519
id_ed25519
is the private key we generated earlier. Its full path is ~/.ssh/id_ed25519
. We add it to the activated SSH agent using the ssh-add
command.
Note: You need to add a config file before using the command on MacOS.
Next, log in to GitHub and store the public the key under SSH (New) key text box. Use this path: Settings->SSH and GPG keys
Now that you have securely connected two remote machines, let's invoke and solve the error fatal: could not read from remote repository.
Source~1: Pushing to non-existing remote
Despite authenticating the local machine, Git and GitHub may fail to understand each other if you attempt to push to a remote repository that does not exist.
Let's build a local repository and attempt to push the changes to a non-existing remote.
Steps
steve@thisHostname:~$ mkdir test_repo && cd test_repo steve@thisHostname:~/test_repo$ git init Initialized empty Git repository in /home/steve/test_repo/.git/ steve@thisHostname:~/test_repo$ cat >> example.py print('Hello world') steve@thisHostname:~/test_repo$ git status On branch main No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) example.py nothing added to commit but untracked files present (use "git add" to track) steve@thisHostname:~/test_repo$ git add . steve@thisHostname:~/test_repo$ git commit -m "Testing 1, 2, 3" [main (root-commit) aae3b47] Testing 1, 2, 3 1 file changed, 1 insertion(+) create mode 100644 example.py steve@thisHostname:~/test_repo$ git log commit aae3b47090a9fc9cc561fee37fd3077864fe573b (HEAD -> main) Author: Stevealila <stevealila25@gmail.com> Date: Tue Feb 7 10:43:37 2023 +0300 Testing 1, 2, 3
We create test_repo
directory and convert it to a Git repository using the git init
command. We then create example.py
file in the working directory before staging and committing the changes.
Problem
Two errors pop up on attempting to push changes to an unknown remote. One of the errors is fatal: Could not read from remote repository.
steve@thisHostname:~/test_repo$ git push origin main fatal: 'origin' does not appear to be a git repository fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
Solved
The solution is to create the repo
and set its URL as the local repository's remote URL.
steve@thisHostname:~/test_repo$ git remote add origin https://github.com/Stevealila/test_repo.git steve@thisHostname:~/test_repo$ git remote set-url origin git@github.com:Stevealila/test_repo.git steve@thisHostname:~/test_repo$ git push origin main Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Writing objects: 100% (3/3), 237 bytes | 237.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 To github.com:Stevealila/test_repo.git * [new branch] main -> main
Now the connection goes through after adding or setting the remote connection.
Source~2: Incorrectly linking through SSH keys
You may also get the error when you fail to connect the machines with the SSH keys correctly.
For example, you could be pushing to or cloning from the remote after connecting the machines with SSH for the first time.
The (local) machine asks, "Are you sure you want to continue connecting (yes/no/[fingerprint])?" You should input 'yes' followed by clicking the enter key. If you input 'no' or leave it blank, the connection fails and throws the error fatal: could not read from remote repository.
Problem
steve@thisHostname:~$ git clone git@github.com:Stevealila/Python.git pyFiles
Cloning into 'pyFiles'...
The authenticity of host 'github.com (140.82.121.4)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
solved
steve@thisHostname:~$ git clone git@github.com:Stevealila/Python.git pyFiles
Cloning into 'pyFiles'...
The authenticity of host 'github.com (140.82.121.4)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 10 (delta 0), reused 4 (delta 0), pack-reused 0
Receiving objects: 100% (10/10), done.
The pull process goes through after we type 'yes'.
Conclusion
You just learned the main causes of the error fatal: could not read from remote repository and how to solve them. Now it is your turn to apply the knowledge.
Thank you very much.
I solved the problem.
Thanks.