Use more than one Git account on the same machine
December 27, 2019
Add more than one Github account
I have multiple git accounts on github.com, gitlab and bitbucket. Each site has different username and email address. So, I need to use
https for the second and third accounts because
ssh doesn’t work with the key I have created for the first account.
This is a common problem we faced. Even if you have one
personal and one
work account on the same computer, you may need to use
https for one.
How to use multiple accounts in one machine :
The first thing we need is to create multiple
SSH key files. One
SSH for each account. Before that, open one terminal and run the below command to check the
SSH keys in your local machine :
ls -al ~/.ssh
It will print something like below :
drwx------ 8 nkd staff 256 Dec 25 18:09 . drwxr-xr-x+ 57 nkd staff 1824 Dec 26 23:30 .. -rw-r--r-- 1 nkd staff 278 Dec 25 18:09 config -rw------- 1 nkd staff 3389 Nov 3 17:54 id_rsa -rw-r--r-- 1 nkd staff 747 Nov 3 17:54 id_rsa.pub -rw-r--r-- 1 nkd staff 1602 Nov 3 21:04 known_hosts
id_rsa is the existing key and if you might have used that key on your existing Git account.
Create one new SSH key :
Now, move to this folder and create one new pair of keys :
cd ~/.ssh ssh-keygen -t rsa -b 4096 -C "your-email"
It will ask you to enter the file name. Enter one different file name as
id_rsa_email and hit enter. Now, it will ask you to enter a passphase. You can hit
enter to leave it blank.
Once done, it will create that new file in that folder.
Create one new config file :
Create one new
config file in the
.ssh folder :
Edit that file with the below content :
# Default github account Host github.com HostName github.com IdentityFile ~/.ssh/id_rsa IdentitiesOnly yes # Second github account Host github-second HostName github.com IdentityFile ~/.ssh/id_rsa_second IdentitiesOnly yes
We have two
rsa files in the
.ssh folder and we are linking both keys to two different
github.com accounts. Use the below command to test if the second key works or not :
ssh -T git@github-second
How to connect :
Now, if your ssh address for the repository is :
Change it to :
to use it with the second rsa file. Make sure that you have added this file on your second Github account.