Skip to content

Instantly share code, notes, and snippets.

@udantha
Last active November 23, 2023 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save udantha/fed5439630eaf4651272f4fba6e1c6a3 to your computer and use it in GitHub Desktop.
Save udantha/fed5439630eaf4651272f4fba6e1c6a3 to your computer and use it in GitHub Desktop.
Git clone with multiple Github accounts in one computer

Multiple GitHub Accounts & SSH Config

A much better and a working solution than this is linked below.

https://gist.github.com/oanhnn/80a89405ab9023894df7

If you have multiple Githib accounts (ex: personal and work) using SSH keys for both accounts can be pretty cumbersome. Following is a simple way to get it done with ssh config.

Prerequists

You need to follow official github guideline to create an ssh keypair, add it to your github account and complete the steps upto "Adding your SSH key to the ssh-agent" in official guide https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#adding-your-ssh-key-to-the-ssh-agent

Steps

  1. Make sure you've completed creating and configuring the SSH as mentioned above.
  2. Open ~/.ssh/config file in your editor. Create one if a file doesn't exist.
  3. Add a host record as following
Host github.com:<YOUR_GITHUB_USERNAME>
  AddKeysToAgent yes
  IdentityFile ~/.ssh/<YOUR_SSH_KEY_FILENAME>

Replace <YOUR_GITHUB_USERNAME> with your desired github username (personal or work) Replace <YOUR_SSH_KEY_FILENAME> with your keyfile name in .ssh folder (ex: id_ed25519)

Add a host record for each of the github account you want to maintain.

Git clone

Git clone would work without any domain name changes to match your ssh condig. Just copy the ssh url and clone away. No special domain names to remember. git clone github.com:<YOUR_GITHUB_USERNAME>/your-repo.git

Please do suggest if you find any of the above needs improvement.

@wickles
Copy link

wickles commented Sep 11, 2023

This did not work for me at all, while the accepted solutions work fine. You must have additional hidden configuration to make this work.

❯ git push
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment