Confgure Multiple Github Accounts On One Machine

How to use different github account for different repository? For instance, we have two github accounts x1 and x2, while x1 for repo1 and x2 for repo2. At the first glance, we can set git config in different repository folder by git config user.name xxx. However, this approach has two drawbacks:

  • Need to config user name/email in every repository
  • In some case, the git user cannot be configured by git config. For example, hexo-deployer-git. Since the git repo is automatically generated by the deployer, it's hard to manually set the user name.

Fortunately, we can leverage SSH config to associate different github accounts with different repos. Define different host entries does the trick: since we login to github via SSH, we use a virtual host as an alias to represent the real host name.

Generating a New SSH Key

# Generating a new SSH key and adding it to the ssh-agent

$ ssh-keygen -t ed25519 -C "your_email@example.com"

Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/user/.ssh/id_ed25519): 
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_ed25519
Your public key has been saved in /home/user/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256: xxx your_email@example.com

Add your SSH private key to the ssh-agent:

$ ssh-add ~/.ssh/id_ed25519
Identity added: /home/user/.ssh/id_ed25519 (your_email@example.com)

Add the Key to GitHub Account

# Adding a new SSH key to your GitHub account

Copy the public key:

$ cat ~/.ssh/id_ed25519.pub
ssh-ed25519 xxxx your_email@example.com

Add the key to github account in github main page: Profile Photo -> Settings -> SSH and GPG keys -> New SSH keys

Edit SSH Config

This is the most important part, create or edit ~/.ssh/config:

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa

Host githubx1
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_x1

Host githubx2
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_x2

Explanations for the config file:

  • Host: the host alias as well as the section name
  • HostName: the real host name to login
  • User: the username for the SSH connection
  • IdentityFile : the SSH private key file generated before

For the above sample config file, assume the key file/github account mapping is:

  • id_rsa -> main@example.com
  • id_rsa_x1 -> x1@example.com
  • id_rsa_x2 -> x2@example.com

Typically, a repo's remote url looks like this:

git@github.com:finisky/TextCNN.git

By default, we will access the repo by identity main@example.com because the repo remote host is github.com. If we want to access the repo by x1@example.com, just change the remote url to:

git@githubx1:finisky/TextCNN.git

Test Connection

Verify the configuration is easy. Test the connection by the following command (notice that the ssh username is git):

$ ssh -T git@githubx1
Hi x1! You've successfully authenticated, but GitHub does not provide shell access.

$ ssh -T git@githubx2
Hi x2! You've successfully authenticated, but GitHub does not provide shell access.

$ ssh -T wrongusername@githubx2
wrongusername@github.com: Permission denied (publickey).

If success, the corresponding GitHub username (aka x1, x2) will be displayed after "Hi".