1

I'm trying to set up multiple Github.com accounts on my computer. I've followed this tutorial and also this one here. However, I'm having issues. I'm working on the Unbuntu Linux subsystem on windows.

My ssh config file is as follows:

# personal git hub
Host github.com
    Hostname github.com
    User git
    IdentityFile ~/.ssh/id_ecdsa_gh

# work git hub
Host github-work
    Hostname github.com
    User git
    IdentityFile ~/.ssh/id_ecdsa

The issue is that if I specify that the host for my personal (primary) github account as github.com and I push a repo associated with this account then everything works fine for this account. However, the work account no longer works and I get a message in terminal saying Please make sure you have the correct access rights and the repository exists.

If I then change my config file and specify the host for personal account as something like personal then it no longer works but my work account will then begin working fine. If I change both so neither Host is called github.com then my work account can push but my personal one is unrecognised. If I set both to github.com then my personal account works again and my work account does not.

Aside from the above tests with changing the host name, I've also tried deleting and updating the keys, which din't work. I've also tried testing that the ssh keys are recognised by git by using ssh -T myname-git and ssh -T work-git. In doing this I get messages telling me Hi [account name] successfully authenticated, but Github does not provide shell access. So the keys both work fine and are recognised by Github. I'm really not sure what's causing this. I've cleared and re-added the keys with ssh-add and I've checked the repos I'm working with are locally associated with the correct accounts and that there are no commits accidentally associated with the wrong accounts. Further I've check that both the global account on windows and the global account on the Linux subsystem are set to the credentials of my personal account. I've also been through the Q&As on stack looking at similar questions, but none of the suggested methods solved this for me.

Can anyone suggest what could be wrong and how I could fix it?

Thanks for reading.

JMcK
  • 100
  • 1
  • 9

2 Answers2

1

The way SSH config files are parsed, the first line/block that matches the hostname is applied, and the rest of the file isn’t processed. The match is done on the Host line; you need to do git clone git://git@github-work/user/repo.git (possibly without the git@) to instruct it to use the second config.

Andrew H
  • 11
  • 3
  • 1
    HI @Andrew H, thanks for your comment. The details on how `config` is read is helpful. I don't see how cloning a remote repo will fix the issue. Can you explain a bit more? – JMcK Jan 25 '22 at 15:17
  • 1
    Ah, sorry! I should've been a bit more clear; I was giving an example of using the `github-work` URL/name in the remote rather than the bare `github.com` - which you worked out in the answer you posted later on :) glad you worked it out! – Andrew H Jan 27 '22 at 08:38
1

I finally figured this out and I'm posting an answer in case anyone get stuck like I did.

Configure the ssh config file as follows:

# personal git hub
Host github.com-personal
    Hostname github.com
    User git
    IdentityFile ~/.ssh/id_ecdsa_gh

# work git hub
Host github.com-work
    Hostname github.com
    User git
    IdentityFile ~/.ssh/id_ecdsa

Then navigate to whatever repo you are working on and modify [remote "origin"] by doing vim .git/config to open the file (or whatever text editor you prefer). then modify this so it corresponds to the correct github key. for example to make it correspond to my work account I would edit it to read as follows:

[remote "origin"]
        url = git@github.com-work:work/myRepo.git

wherein the github.com-work or github.com-personal corresponds to the names given in the Host part of your config file for the appropriate key.

JMcK
  • 100
  • 1
  • 9