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 A-account@git.com 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 "your_email@youremail.com"

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

~/.ssh/id_rsa_account1
~/.ssh/id_rsa_account2

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
Host github.com-account1
    HostName github.com
    User account1
    IdentityFile ~/.ssh/id_rsa_account1

#Github account2
Host github.com-account2
    HostName github.com
    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 = git@github.com-account1:account1/gfs.git

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

[remote "origin"]
            url = git@github.com-account1:invitedByUserName/gfs.git
Step-6.2: Navigate to account2’s project and update host:
[remote "origin"]
        url = git@github.com-account2:account2/gfs.git

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 user.name "account1"
git config user.email "account1@domain.com" 

Navigate to account2 project and run these:

git config user.name "account2"
git config user.email "acccount2@doamin.com" 

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

git clone git@github.com-account2: