If you have more than one Github account, you may want to use them on the same machine without typing your username and password every time you push or pull. In this post, I will show you how to use ssh keys to manage multiple Github accounts on a single machine.
This blog post will guide you through the process of setting up multiple Github accounts using SSH, granting you seamless access without ever entering another password. Let's unleash your inner juggling act!
Step 1: Generate the Keys
The first step is to generate SSH keys for each of your GitHub accounts Each account gets its own private key (kept safe on your machine) and a public key (shared with Github for verification).
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/your_email -P ""
# Or
ssh-keygen -t ed25519 -b 4096 -C "your_email@example.com" -f ~/.ssh/your_email -P ""
Repeat this incantation for each account, giving each key a relevant label.
Step 2: Add SSH Keys to the SSH Agent
Start the SSH agent and add the keys:
eval "$(ssh-agent -s)"
# For the first GitHub account
ssh-add ~/.ssh/id_rsa
# For the second GitHub account
ssh-add ~/.ssh/id_rsa_second
Step 3: Add SSH Keys to GitHub
Copy the content of each public key:
cat ~/.ssh/id_rsa.pub # Copy this for the first account
cat ~/.ssh/id_rsa_second.pub # Copy this for the second account
Go to your Github account settings > SSH and GPG keys > New SSH key, and paste the respective key for each account.
Open browser and navigate to Github account settings, navigate to SSH and GPG keys
and click New SSH key.
Paste the public key for each account and give it a fitting title.
Step 4: Create SSH Config File
Create or edit the ~/.ssh/config
file:
vi ~/.ssh/config
Host github-work
HostName github.com
IdentityFile ~/.ssh/work_key
IdentitiesOnly yes
Host github-personal
HostName github.com
IdentityFile ~/.ssh/personal_key
IdentitiesOnly yes
Replace work_key
and personal_key
with your actual paths. Each Host block defines a unique alias (e.g., "github-work") for easier access.
Step 5: Clone Repositories
When cloning repositories, use the appropriate hostname for each account
git clone git@github-work:your_company/project.git
git push -u origin master
There are no prompts. You'll be seamlessly switching between your Github identities.
Now you have successfully set up multiple GitHub accounts with multiple SSH keys on your machine. You can seamlessly switch between accounts and contribute to different projects without any conflicts.
Remember
- Keep your private keys safe and secure.
- Consider passphrase protection for an extra layer of security.
- Don't share your private keys with anyone!