N accounts on 1 machine
If you have two GitHub accounts, such as github.com/gopx-office and github.com/gopx-personal, you can easily manage both on the same machine.
Tip: This method works for more than two accounts as well!
Steps to follow#
- Generate SSH keys for each account
- Add SSH keys to the SSH agent
- Add SSH keys to GitHub
- Create an SSH config file with host entries
- Clone repositories using the correct account
1. Generate SSH Keys#
Navigate to your .ssh
directory:
cd ~/.ssh
Generate SSH keys for each account:
ssh-keygen -t rsa -C "office-email@gmail.com" -f "github-gopx-office"
ssh-keygen -t rsa -C "personal-email@gmail.com" -f "github-gopx-personal"
After entering the command the terminal will ask for passphrase, leave it empty and proceed.
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/username/.ssh/github-gopx-office):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/username/.ssh/github-gopx-office.
Your public key has been saved in /Users/username/.ssh/github-gopx-office.pub.
The key fingerprint is:
SHA256:XXXXXXXXXXXXXX your-office-email@gmail.com
The key's randomart image is:
+---[RSA 2048]----+
| ... |
| .o . |
| o. . |
| ... .. |
| ... . |
| ... . |
| .o.. . |
| ..o .. |
+----[SHA256]-----+
After generating the keys, your .ssh
folder will contain both the public key github-gopx-office.pub
and the private key github-gopx-office
. The public key has a .pub
extension, while the private key does not.
2. Add SSH Keys to the Agent#
Add your SSH keys to the SSH agent:
ssh-add -K ~/.ssh/github-gopx-office
ssh-add -K ~/.ssh/github-gopx-personal
Learn more about adding keys to the SSH agent, check out - Generating a new SSH key and adding it to the ssh-agent.
3. Add SSH Keys to GitHub#
Copy and add your public keys to GitHub. For doing this we need to:
1. Copy the public key#
We can copy the public key either by opening the github-gopx-office.pub file in vim and then copying the content of it.
vim ~/.ssh/github-gopx-office.pub
vim ~/.ssh/github-gopx-personal.pub
OR
We can directly copy the content of the public key file in the clipboard.
pbcopy < ~/.ssh/github-gopx-office.pub
pbcopy < ~/.ssh/github-gopx-personal.pub
2. Paste the public key on GitHub#
- Sign in to GitHub Account
- Goto Settings > SSH and GPG keys > New SSH Key
- Paste your copied public key and give it a Title of your choice
OR
- Sign in to GitHub
- Paste this link in your browser https://github.com/settings/keys
- Click on New SSH Key and paste your copied key
4. Configure SSH for Multiple Accounts#
Create or open the SSH config file:
touch ~/.ssh/config
open ~/.ssh/config
Add the following configurations:
# Office account
Host github.com-gopx-office
HostName github.com
User git
IdentityFile ~/.ssh/github-gopx-office
# Personal account
Host github.com-gopx-personal
HostName github.com
User git
IdentityFile ~/.ssh/github-gopx-personal
5. Clone Repositories#
To clone repositories, use the correct SSH host for the respective account:
git clone git@github.com-gopx-personal:{owner}/{repo}.git
git clone git@github.com-gopx-office:{owner}/{repo}.git
Final Configuration#
For each repository, set the appropriate user information:
git config user.email "office-email@gmail.com"
git config user.name "Gopx Office"
Repeat for the personal account:
git config user.email "personal-email@gmail.com"
git config user.name "Gopx Personal"
To ensure proper push/pull commands, set the remote origin:
git remote add origin git@github.com-gopx-personal:gopx-personal/repo.git
git remote add origin git@github.com-gopx-office:gopx-office/repo.git
You can now use git push and git pull as usual.