How to Use Multiple GitHub Accounts on a Single Machine
Assume that you have two accounts on GitHub: https://github.com/gleniox-personal and https://github.com/gleniox-work.
The next step is to configure your computer (in this case, Windows) so that it can communicate with both of the GitHub accounts.
There are five simple steps to setup:
Step 1: Set up each account’s SSH key
Step 2: Configure SSH Agent with SSH keys
Step 3: Update GitHub with the SSH public key
Step 4: Make Host Entries and a Configuration File
Step 5: Using distinct accounts to clone GitHub repositories
Step 1: Set up each account’s SSH key
Create SSH keys for all accounts:
Make sure your current folder is your \.ssh folder. To create a \.ssh folder, navigate to C:\Users\[username] (e.g. C:\Users\gleniox).
The syntax for creating a unique SSH key for an account is:
ssh-keygen -t rsa -C “your-email-address” -f “github-username”
Note:
- C stands for comment, which helps identify your ssh key.
- -f stands for the file name where your ssh key is saved.
Currently generating SSH keys for your two accounts:
Run:
ssh-keygen -t rsa -C “work_email@mail.com”. -f “github-gleniox-work”
ssh-keygen -t rsa -C “personal_email@mail.com”. -f “github-gleniox-personal”
Notice that gleniox-work and gleniox-personal are the usernames for my GitHub accounts, which correlate to the email addresses work_email@mail.com and personal_email@mail.com, respectively.
After entering the command, the terminal will prompt you for a passphrase (which you may leave empty if you decide) and then proceed.
After adding keys to your \.ssh folder, a public and private key will be generated.
The public key will include an extension. The public and private keys will be present with the same name that you specified following the -f option in the above command.
(For example, github-gleniox-personal and github-gleniox-personal.pub)
Step 2: Configure SSH Agent with SSH keys
Open your Windows terminal and start the ssh-agent service:
Get-Service -Name ssh-agent | Set-Service -Startup Type Manual
Start-Service ssh-agent
More information on adding keys to SSH Agent can be found here: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
Add SSH keys to the SSH Agent:
We now have the keys, but they cannot be utilized until they are added to the SSH agent.
Run:
ssh-add C:/Users/[username]/.ssh /GitHub-Gleniox-Work
ssh-add C:/Users/[username]/.ssh /GitHub-Gleniox-Personal
Step 3: Update GitHub with the SSH public key
Add SSH public key to GitHub:
The next step is to add our public key (which we generated in the previous step) to the respective GitHub accounts.
1. Copy the public key.
To access your public key, run:
cat C:/Users/[username]/.ssh /github-gleniox-work.pub
cat C:/Users/[username]/.ssh/github-gleniox-personal.pub
2. Paste the public key on GitHub.
3. Sign in to your GitHub account.
4. Next, log in to your second GitHub account, select Settings from the drop-down menu next to your profile picture in the top right corner, and then SSH and GPG keys.
5. Enter your copied public key and give it a title of your choice.
Step 4: Make Host Entries and a Configuration File
Create a configuration file and add host entries:
The C:/Users/[username]/.ssh/config file allows us to configure numerous SSH parameters.
If the configuration file does not already exist, create it (make sure you are in the C:/Users/[username]/.ssh folder).
Now we need to add these lines to the file, one for each account we created before:
#gleniox-work account
Host github.com-gleniox-work
HostName github.com
User git
IdentityFile ~/.ssh/github-gleniox-work
# gleniox-personal account
Host github.com-gleniox-personal
HostName github.com
User git
IdentityFile ~/.ssh/github-gleniox-personal
Step 5: Using distinct accounts to clone GitHub repositories
Cloning GitHub repositories using various accounts:
So we’ve completed our preparations, and it’s time to put them into action. We’ll clone a repository with one of the accounts we’ve added.
Create a new project folder where you want to clone your repository and navigate to it from the console.
For example, I’m creating a repository on my personal GitHub account and naming it XptoRepo. Now, to clone the repository, run the following command:
git clone git@github.com-{your-username}:{owner-user-name}/{the-repo-name}.git
(e.g. git clone git@github.com-gleniox-personal:gleniox-personal/XptoRepo.git)
To ensure that our commits and pushes from each repository on the system are made by the correct GitHub user, we must now configure user.email and user.name in each newly cloned repository.
To accomplish this, run the following commands:
git config user.email “work_email@mail.com “
git config user.name “gleniox_work”
git config user.email “personal_email@mail.com “
git config user.name “ gleniox_personal”
Choose the relevant pair for your repository.
To push or pull to the correct account, add the remote origin to the project:
git remote add origin git@github.com-gleniox-personal:gleniox-personal
git remote add origin git@github.com-gleniox-work:gleniox-work
Now you can use:
git push
git pull
Have fun! :)