1

Whenever I start a session on my PC and try to git fetch my remote repository, I get this error:

ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

What I do each time, then, is execute the following commands:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_github
ssh -i ~/.ssh/id_rsa_github -T [email protected]

Once I've executed the commands above, git fetch finally works properly. However, I don't want to repeat the same steps over and over again every time I turn my PC on. How do I solve this issue? I've already tried putting those three commands inside ~/.bashrc, but that doesn't work.

In case you're wondering, I already added an SSH key to my GitHub account pasting the contents of the public key ~/.ssh/id_rsa_github.pub, but I still have that issue.

3 Answers 3

1

Add

host github.com
 HostName github.com
 IdentityFile ~/.ssh/id_rsa_github

to your ssh config file (located at ~/.ssh/config)

If you want to specify the identity for each git repo individually, you can modify the ssh command for that specific repo:

git config core.sshCommand 'ssh -i ~/.ssh/id_rsa_github'

(run this command from inside the git repository dir)

Sign up to request clarification or add additional context in comments.

4 Comments

What if I have more than one private key (one for each different GitHub account)?
I've updated my answer. You can set an individual identity for each git repo.
That config option is exactly what I was missing. Thanks!
In addition to that, seems like in the Mac you can configure to avoid having to type the passphrase every time. Just be careful when choosing the host (work vs personal).
0

It seems the keys are getting lost each time you start a session. You need to add the keys permanently to your agent. For reference on adding the key permanently, you can check the following link - https://stackoverflow.com/a/4246809/12680971

Comments

0

The environment variable, SSH_AUTH_SOCK, will be set when ssh-agent initiates. So something like this in your .bashrc would be a good idea:

[ -z "$SSH_AUTH_SOCK" ] && eval "$(ssh-agent -s)"

This will start a new ssh-agent in the background if it is not already running.

Further, in your ~/.ssh/config file you should have something like this:

AddKeysToAgent yes

This should add all your loaded keys to your ssh-agent as if you were running ssh-add to every key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.