How to use two ssh keys to access two different GitHub account.

I tried to use two ssh keys for my two GitHub accounts, one for my personal account, one for my school account.

However, I screwed the ssh credential for GitHub verification. When I tried to use my personal account to push my code, I got permission denied to B-account.

I took some time searching online and finally figure it out:

Step-1: Create two ssh key pairs:

ssh-keygen -t rsa -C ""

Step-2: It will create two ssh keys here:


Step-3: Now we need to add these keys:

ssh-add ~/.ssh/id_rsa_account2
ssh-add ~/.ssh/id_rsa_account1

You can see the added keys list by using this command:

ssh-add -l

You can remove old cached keys by this command:

ssh-add -D

Step-4: Modify the ssh config

cd ~/.ssh/
vim config

Step-5: Add this to config file:

#Github account1
    User account1
    IdentityFile ~/.ssh/id_rsa_account1

#Github account2
    User account2
    IdentityFile ~/.ssh/id_rsa_account2

Step-6: Update your .git/config file:

Step-6.1: Navigate to account1's project and update host:
[remote "origin"]
        url =

If you are invited by some other user in their git Repository. Then you need to update the host like this:

[remote "origin"]
            url =
Step-6.2: Navigate to account2's project and update host:
[remote "origin"]
        url =

Step-7: Update user name and email for each repository separately if required this is not an amendatory step:

Navigate to account1 project and run these:

git config "account1"
git config "" 

Navigate to account2 project and run these:

git config "account2"
git config "" 

Step-8 If you want to clone your second account's repository:

git clone