Working with multiple GitHub accounts on the same computer can seem tricky, but it's actually quite manageable with the right approach. Here's a simplified guide to help you set it up:
How the connection happens to the Git host
GitHub uses SSH keys to verify your identity. When you try to connect to GitHub, your computer offers an SSH key. If GitHub recognizes it, you're in! The problem arises when you have multiple accounts with different keys – GitHub can get confused about which key belongs to which account.
The Solution: SSH Config File
The magic lies in a file called config
within your .ssh
directory. This file lets you create different profiles for each of your GitHub accounts. Think of it like giving each account a unique nickname that your computer and GitHub both understand.
Steps
-
Generate SSH Keys: For each GitHub account, generate a unique SSH key pair. Use the
ssh-keygen
command in your terminal. The important note here is that each file name should be different. Refer Github Docs.
For example:
ssh-keygen -t ed25519 -C "your_work_email@example.com" -f "id_ed25519_work" ssh-keygen -t ed25519 -C "your_personal_email@example.com" -f "id_ed25519_personal"
Add SSH Keys to GitHub: Add the public key of each key pair to the corresponding GitHub account settings.
Refer Github Docs-
Create/Edit SSH Config File: Open (or create if it doesn't exist) the
config
file located in your.ssh
directory (usually~/.ssh/config
).
Add a separate block for each account, like this:
# Work GitHub Host work_github HostName github.com User git IdentityFile ~/.ssh/id_ed25519_work # Personal GitHub Host personal_github HostName github.com User git IdentityFile ~/.ssh/id_ed25519_personal
-
Clone Repositories with Specific Accounts: When cloning a repository, use the
Host
you defined in yourconfig
file:
git clone git@work_github:your-organization/work-repository.git git clone git@personal_github:your-username/personal-repository.git
Handling Different GitHub Domains
If you're using both github.com
and an enterprise GitHub (e.g., github.yourcompany.com
), the process is similar. The main difference is in the HostName
entry within your config
file.
For example:
# Enterprise Account
Host github.yourcompany.com
HostName github.yourcompany.com
User git
IdentityFile ~/.ssh/id_ed25519_enterprise
Important Notes:
- Security: Keep your private keys safe! Treat them like passwords.
-
Git Configuration: You might need to configure
user.name
anduser.email
for each repository to ensure your commits are attributed to the correct account. By following these steps, you can seamlessly manage multiple GitHub accounts on your machine without any conflicts. Happy coding!
P.S.: This tutorial is for other Git providers like GitLab, Bitbucket, etc as well, with minor changes in steps.
Top comments (0)