0

A remote connection from a local git repository to GitHub repository, with registered email email-1@email.com, is in place. Connection is working with no issues.

A second remote connection is being made to a different GitHub repository under a different GitHub account registered with a different email address (email-2). Firstly an SSH key is made of type ed25519 using:

$ ssh-keygen -t ed25519 -C "email-2@email.com"

A single existing ssh keys exists in the default repository (that for email-1@email.com). As such a name is given to the new key; "passion". Viewing the .ssh folders show the public and private keys of the new ssh key:

New SSH Key

This key is then added to the GitHub account: Settings -> SSH & GPG Keys -> New SSH Key; the contents of passion.pub are then copied into "key" which is of type "Authentication Key".

This key is then added to the agent:

$ eval "$(ssh-agent -s)" 
$ ssh-add ~/.ssh/passion

Both keys are shown to be present in the agent:

Two Keys in Agent

This is the first time creating a secondary remote with secondary key. As such it is necessary to create the file ~/.ssh/config with IdentitiesOnly set to yes. Then add an alias to point the agent to the secondary key to use for the secondary remote.

cd ..ssh
touch config

The alias for the secondary remote is then entered.

IdentitiesOnly yes
Host passion-origin
    HostName git@github.com:account/passion
    User email_2@email.com
    IdentityFile ~/.ssh/passion

A new local folder is created with the same name as that of the GitHub repository. Then a local git repository is created therein using:

git init

A new remote is added to the GitHub repository for the account registered with email-2@email.com using the ssh address:

git remote add passion-origin git@github.com:account/passion

This is found to be successfully in place:

git remote -v

New Remote in Place

It is then attempted to pull from the new remote but this returns an error:

Error on Pull

What is the error in my approach?

acolls_badger
  • 423
  • 1
  • 9
  • 29
  • 1
    When using the ssh agent, if you don't *restrict* ssh to sending *only* the desired key, it will try *all* the keys it gets from the agent, in the order the agent hands them over. So if the agent hands over the key that lets you into GitHub as "user A" first, you'll get *into* GitHub as "user A", and have permission to access user A's repositories, but no permission for user B's repositories. That will be true regardless of what else you do, because you're always sending the user A key first and it always works and that makes you user A to GitHub. – torek Nov 22 '22 at 09:29
  • 1
    The usual solution to this problem is to set `IdentitiesOnly yes` in your *ssh* configuration. This tells it to look at the `IdentityFile` line(s) for that host, and only those key(s) that match the file(s) listed here go over. So now you can pick which *key* to send, based on which *ssh host name* you use. With `Host gh-user-a` you send the user-A key, and with `Host gh-user-b` you send the user-B key, and hence if your URL says `gh-user-a:user-a/repo-1` you will clone that as user B, but if your URL says `gh-user-b:user-b/repo-3` you will clone that as user B. – torek Nov 22 '22 at 09:32
  • 1
    You can, however, just remove and add the identities to/from the ssh agent to control which key(s) you send, in which order. – torek Nov 22 '22 at 09:32
  • Thanks, this pointed me towards ~/.ssh/config. In my case this file didn't exist so I then created it and added an alias as per this post https://stackoverflow.com/questions/49587932/i-dont-have-ssh-config-file-but-multiple-github-accounts-works-how. Unfortunately this still isn't working for me. I'll edit the edit for where the situation now sits. – acolls_badger Nov 25 '22 at 08:17

0 Answers0