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 git@github.com

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.

Francesco Cariaggi
  • 688
  • 2
  • 9
  • 23

3 Answers3

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)

sapisch
  • 535
  • 3
  • 14
  • What if I have more than one private key (one for each different GitHub account)? – Francesco Cariaggi May 12 '20 at 08:25
  • I've updated my answer. You can set an individual identity for each git repo. – sapisch May 12 '20 at 18:00
  • That config option is exactly what I was missing. Thanks! – Francesco Cariaggi May 13 '20 at 08:38
  • In addition to that, seems like in the Mac you can configure to [avoid having to type the passphrase every time](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#adding-your-ssh-key-to-the-ssh-agent). Just be careful when choosing the host (work vs personal). – marcelocra Aug 24 '22 at 16:53
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

Nafiz Ahmed
  • 567
  • 3
  • 10
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.

adep7
  • 23
  • 2
  • 7