Search code examples
linuxgithubkey-management

configure multiple git accounts with config file only recognises first GitHub account and ignores second


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.


Solution

  • 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 = [email protected]: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.