07 August 2025

How to use Github SSH when working with more than one Github account?

I had earlier written about how to use an SSH key to clone, push and pull from Github.When working with more than one Github account, Git automatically considers only the first key even if you add a second key file in the ~/.ssh folder. To make Git recognize the right file, you need to add a new config file in that folder.


First add a new ed25519 file (like how it's normally done) for the second account and give it a new name like id2_ed25519.
Then create a new file named config.

Now add this exact text into the config file:

# First GitHub account
Host github.com
    HostName
github.com
    User git
    IdentityFile ~/.ssh/
id_ed25519

# Second GitHub account
Host
github-second
    HostName github.com
    User git
    IdentityFile ~/.ssh/
id2_ed25519

If there are any errors related to file permissions, change the permission using chmod 600 ~/.ssh/id2_ed25519 but it's unlikely there would be such issues.

That's it. Now to clone from the first account you can use the equivalent of:

git clone git@github.com:username1/gitRepository1.git

To clone from the second account you can use the equivalent of:

git clone git@github-second:username2/gitRepository2.git


No comments: