Using multiple SSH keys for different GitHub accounts

Consider that these are the normal steps to add a new repo to github:

echo "# anotherproject" >>
git init
git add
git commit -m "first commit"
git branch -M main
git remote add origin
git push -u origin main

But let's assume you just created another GitHub account (anotheraccount) and will be using it for the first time, adding new repo anotherproject.

First we need to create a new SSH key and add it to anotheraccount. GitHub won't allow you to associate a single SSH key with multiple accounts so you will not be able to use your existing SSH key, assuming you've already added it to your existing GitHub account (yourname). You must create another SSH key for anotheraccount. But how will Git know which one to use? Keep reading to find out...

Change to the ssh directory

$ cd ~/.ssh

Generate a new ssh key using email associated with second github account

~/.ssh$ ssh-keygen -t ed25519 -C ""

When prompted, give it the name id_ed25519_anotheraccount. Do not use the default name (id_ed25519) or you'll overwrite your existing SSH key. Now you should have two public/private SSH key pairs:

~/.ssh$ ls
id_ed25519  id_ed25519_anotheraccount  known_hosts

Add the new private key (id_ed25519_anotheraccount) to the SSH agent:

~/.ssh$ ssh-add id_ed25519_anotheraccount

Add the new public key ( to your new GitHub account (Settings -> SSH and GPG keys -> New SSH Key).

Modify the SSH config:

~/.ssh$ touch config
~/.ssh$ subl -a config


#yourname account
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes

    IdentityFile ~/.ssh/id_ed25519_anotheraccount
    IdentitiesOnly yes

Now the trick is to use "" in place of "" if you need to force it to use the new SSH key.


$ git remote add origin

Alternatively you can set the host in the git config file:

[remote "origin"]
        url =

And don't forget to set your name and email when you switch accounts:

$ git config --global "anotheraccount"
$ git config --global ""


How to display git config
$ git config --list Name
How to set git settings
$ git config --global
$ git config --global "Your Name"
How to unset a git setting (use case: removing a second
$ git config --global --unset "Your Name"