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 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 ""

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/
The key fingerprint is:
SHA256: xxx

Add your SSH private key to the ssh-agent:

$ ssh-add ~/.ssh/id_ed25519
Identity added: /home/user/.ssh/id_ed25519 (

Add the Key to GitHub Account

# Adding a new SSH key to your GitHub account

Copy the public key:

$ cat ~/.ssh/
ssh-ed25519 xxxx

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:

    User git
    IdentityFile ~/.ssh/id_rsa

Host githubx1
    User git
    IdentityFile ~/.ssh/id_rsa_x1

Host githubx2
    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 ->
  • id_rsa_x1 ->
  • id_rsa_x2 ->

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

By default, we will access the repo by identity because the repo remote host is If we want to access the repo by, just change the remote url to:


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 Permission denied (publickey).

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