Francesco Cariaggi
Francesco Cariaggi

Reputation: 838

Problems when using git with ssh

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.

Upvotes: 1

Views: 1268

Answers (3)

sapisch
sapisch

Reputation: 555

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)

Upvotes: 1

adep7
adep7

Reputation: 23

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.

Upvotes: 0

Nafiz Ahmed
Nafiz Ahmed

Reputation: 567

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

Upvotes: 0

Related Questions