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 Afirstname.lastname@example.org 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 "email@example.com"
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:
You can remove old cached keys by this command:
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 = firstname.lastname@example.org: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 = email@example.com:invitedByUserName/gfs.git
Step-6.2: Navigate to account2's project and update host:
[remote "origin"] url = firstname.lastname@example.org: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 "email@example.com"
Navigate to account2 project and run these:
git config user.name "account2" git config user.email "firstname.lastname@example.org"
Step-8 If you want to clone your second account's repository:
git clone email@example.com: