If you are facing issue with using multiple GitHub accounts in your device, let me help you out.
Here are few simplified steps to setup multiple GitHub account.
1. Generate SSH keys for each account:
You need to create SSH key for each account .i.e.If you are using two github accounts then you need to create two SSH keys. You can create SSH key by running following script in your terminal:
- Fist change your directory :
cd ~/.ssh
- Then generate SSH key :
ssh-keygen -t rsa -b 4096 -C "your-email-id@xyz.com"
Here, you have to replace “your-email-id@xyz.com” with your email account associate to your GitHub.
Then above script will create a public/private key.
Further, in terminal they will ask to “enter the file in which you save the key”
( you can give any filename ,since I am creating this for personal GitHub account so I will name it “personal”).
Moreover, they will ask for paraphrase which is optional just skip by pressing enter.
You can check if the key is created or not with following script :
ls
There two files will be displayed “personal” and “personal.pub”
Just check the SSH key with following script:
cat personal.pub
2. Add SSH Key to Github
Copy the SSH key displayed in the terminal and paste it to your personal github account SSH key which you will find in github=>settings=>SSH and GPG keys . And give it a title “personal”.
3. Create a configuration file
Next you need to create configuration file for SSH to specify different host and SSH keys to users for different github accounts. You can use following script to create new configuration file :
nano ~/.ssh/config
In config file you need to add the following lines :
# Account 1
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/personal
# Account 2
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/work
Replace Host as “github.com-personal” and IdentifyFile as “~/.ssh/personal” depending on your host name you want to keep.
Make sure that the IdentityFile parameter points to the correct SSH key you have created for each account.
Repeat same steps from step1 to step 3 for second account.
4. Clone repositories
Next you need to clone the repository which you want to use .
Open terminal and navigate to folder where you want to clone the repository. Then copy the SSH key of your repository from your GitHub account and clone it :
git clone git@github.com-account1:<account1-username>/<repository-name>.git
Note:
while cloning update “git@github.com-account1” to “git@github.com-personal” as I have given host as “personal” in config file . Rest will be same as copied SSH key of your repository.