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:
Generate SSH keys for each account:
After entering the command the terminal will ask for passphrase, leave it empty and proceed.
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:
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.
OR
We can directly copy the content of the public key file in the clipboard.
#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:
Add the following configurations:
#5. Clone Repositories
To clone repositories, use the correct SSH host for the respective account:
#Final Configuration
For each repository, set the appropriate user information:
Repeat for the personal account:
To ensure proper push/pull commands, set the remote origin:
You can now use git push and git pull as usual.