Managing Multiple Github Accounts on a Single Machine
A Step-by-Step Guide to Setting Up and Using SSH Keys
Prerequisite:
You should have a GitHub account.
You should have a basic knowledge of how to create repositories, what is git and GitHub and how to use them?
This article is for someone who wants to add their personal, and professional github accounts on the same machine.
To set up multiple GitHub accounts on a single machine, you need to use SSH keys. SSH keys are used to authenticate the connection between your computer and GitHub and secure the data transmission.
Follow these steps to set up multiple GitHub accounts on a single machine:
- Create a folder named
.ssh
in your home directory using the following command:
cd ~
mkdir .ssh
Generate SSH keys for each of the two accounts using the following commands:
ssh-keygen -t rsa -P "" -f person1 ssh-keygen -t rsa -P "" -f person2
It will create the following files inside
.ssh
folderperson1 person1.pub person2 person2.pub
the
.pub
means ->public_key
that is used for authenticationYou can see the content of
.pub
files usingcat
command or you can open it innotepad
.Add the public keys to their respective GitHub accounts by copying the contents of the public key files and adding them as new SSH keys in GitHub. To do this:
Go to your GitHub account.
Open settings.
Inside SSH and GPG keys, click on the New SSH key.
Copy the data from
person1.pub
orperson2.pub
(depending on the account) from your computer and paste it into the key section in GitHub.Set the title as
person1
orperson2
(depending on the account).Click Add SSH Key.
Create a config file in the
.ssh
folder using the following command:touch config
Edit the config file to specify the location of the private keys for each of the accounts: ( Just paste the following text and change
person1
andperson2
as yours that you used while creating )Host person1 HostName github.com User git IdentityFile ~/.ssh/person1 Host person2 HostName github.com User git IdentityFile ~/.ssh/person2
To clone a repository, use the following commands:
# cloning as person1: git clone person1:<github-username-1>/repo1.git # cloning as person2: git clone person2:<github-username-2>/repo2.git
To set the correct authority for each repository, set the user name and email address for each repository separately using the following commands:
# For Person1: git config user.name person1 git config user.email email_id_of_person1 # For Person2: git config user.name person2 git config user.email email_id_of_person2
By following these steps, you should be able to successfully set up multiple GitHub accounts on a single machine.
Sometimes there occurs a problem in setting remote origin:
Using the following command might solve the problem:
If a GitHub repository looks like this:-
https://github.com/username/reponame.git
then for adding it as a remote origin, the following command can be used :
# for person1: git remote add origin person1:username/reponame.git # for person2: git remote add origin person2:username/reponame.git
That's all about it.
Hope you find the blog useful. If yes, then do upvote it.
Thank you for reading!!