Managing Multiple Github Accounts on a Single Machine
A Step-by-Step Guide to Setting Up and Using SSH Keys
3 min read
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
.sshin 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
person1 person1.pub person2 person2.pub
public_keythat is used for authentication
You can see the content of
catcommand or you can open it in
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.
Inside SSH and GPG keys, click on the New SSH key.
Copy the data from
person2.pub(depending on the account) from your computer and paste it into the key section in GitHub.
Set the title as
person2(depending on the account).
Click Add SSH Key.
Create a config file in the
.sshfolder using the following command:
Edit the config file to specify the location of the private keys for each of the accounts: ( Just paste the following text and change
person2as 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.gitthen 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!!