Generate SSH Key
For each account, generate a separate SSH key:
ssh-keygen -t ed25519 -C "your-email-for-first-account" -f ~/.ssh/id_ed25519_first
ssh-keygen -t ed25519 -C "your-email-for-second-account" -f ~/.ssh/id_ed25519_second
-
-t ed25519
: Specifies the type of key (ed25519). -
-C "your-email-for-first-account"
: Adds a comment for identification. -
-f ~/.ssh/id_ed25519_first
: Specifies the file name for the private key (~/.ssh/id_ed25519_first
) and the public key will automatically be named~/.ssh/id_ed25519_first.pub
.
Add SSH Keys to the Agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_first
ssh-add ~/.ssh/id_ed25519_second
List Existing SSH Keys
Run the following command to list your SSH keys:
ls ~/.ssh
You should see files like id_ed25519
, id_ed25519.pub
, id_ed25519_first
, id_ed25519_second
, or similar. The .pub files are the public keys.
Add the Public Keys to GitHub
Copy each public key to GitHub:
cat ~/.ssh/id_ed25519_first.pub
cat ~/.ssh/id_ed25519_second.pub
Add these keys to the respective GitHub accounts under Settings > SSH and GPG keys.
Configure ~/.ssh/config
Create or edit the ~/.ssh/config
file to define host aliases:
nano ~/.ssh/config
Add the following lines:
Host github-first
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_first
Host github-second
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_second
Save the file.
Test SSH Keys
Use the following command to test your SSH keys with GitHub.
For the first account:
ssh -T git@github-first
You should see a message like:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Check for the second account too!
If you encounter any issues, ensure:
- The correct SSH keys are added to the agent.
- The
~/.ssh/config
file is correctly set up.
Clone Repositories
Use the alias for the desired account:
git clone git@github-first:username/repository.git
git clone git@github-second:username/repository.git
Update an Existing Repository's Remote
If you're switching remotes:
git remote set-url origin git@github-first:username/repository.git
git remote set-url origin git@github-second:username/repository.git
Push Code from VS Code(or any code editor)
1. Open the repository folder in VS Code.
2. Ensure the correct remote is set:
git remote -v
3. Commit and push code:
git add .
git commit -m "Your commit message"
git push origin main
Top comments (0)