Juggling work and personal code across different platforms like GitHub, GitLab, and Bitbucket can be messy. Using a single SSH key for everything is a security risk, and managing multiple keys manually is a pain. This guide shows you how to configure your SSH to handle multiple accounts seamlessly.
Why This Matters
- Keep identities separate: Prevent work emails from appearing on personal projects.
- Security: Isolate access; if one key is compromised, others remain safe.
- Platform flexibility: Use GitHub for open source, GitLab for private work, and Bitbucket for enterprise easily.
Step 1: Generate SSH Keys for Each Account
Create a unique key for every account/platform you use.
# Navigate to SSH directory
cd ~/.ssh
# Generate key for Personal GitHub
ssh-keygen -t ed25519 -C "personal@email.com"
# Save as: id_github_personal
# Generate key for Work GitLab
ssh-keygen -t ed25519 -C "work@company.com"
# Save as: id_gitlab_work
# Generate key for Bitbucket
ssh-keygen -t ed25519 -C "dev@company.com"
# Save as: id_bitbucket_devStep 2: Add Public Keys to Respective Platforms
Copy each public key (.pub file) and add it to the correct provider.
# Example for Mac/Linux
cat ~/.ssh/id_github_personal.pub | pbcopy- GitHub: Settings -> SSH and GPG keys -> New SSH Key
- GitLab: Preferences -> SSH Keys -> Add new key
- Bitbucket: Personal settings -> SSH keys -> Add key
Step 3: Create the SSH Config File
This is the magic step. We map aliases to specific keys.
# Create or edit the config file
nano ~/.ssh/configAdd the following configuration blocks:
# Personal GitHub Account
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_github_personal
# Work GitLab Account
Host gitlab.com-work
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_gitlab_work
# Bitbucket Account
Host bitbucket.org-dev
HostName bitbucket.org
User git
IdentityFile ~/.ssh/id_bitbucket_devStep 4: Test Connections
Verify each alias connects to the correct account.
ssh -T git@github.com-personal
# Expected: Hi personal-username! ...
ssh -T git@gitlab.com-work
# Expected: Welcome to GitLab, @work-username! ...
ssh -T git@bitbucket.org-dev
# Expected: logged in as work-usernameUsing the Aliases
When cloning or setting remotes, replace the standard hostname with your custom alias.
Cloning Repositories
# Instead of: git clone git@github.com:user/repo.git
git clone git@github.com-personal:user/repo.git
# GitLab
git clone git@gitlab.com-work:group/project.git
# Bitbucket
git clone git@bitbucket.org-dev:workspace/repo.gitUpdating Existing Repos
If you cloned a repo using the default URL and want to switch to your alias:
# Navigate to repo
cd ~/projects/my-project
# Update remote URL
git remote set-url origin git@github.com-personal:user/repo.git⚠️ Important Reminders
- Git User Config: SSH handles authentication, but Git still needs to know who you are (
user.nameanduser.email). Ensure these are set correctly per repository (see our guide onincludeIffor automation). - Key Permissions: Ensure your private keys are not readable by others:
chmod 600 ~/.ssh/id_*.
Pro Tips
- Use descriptive aliases like
gh-personalorgl-workto save typing. - If you encounter "Permission denied (publickey)", use
ssh-add -lto check if your keys are loaded. - This setup works seamlessly with VS Code and other Git clients.
Now you can push to GitHub, GitLab, and Bitbucket without ever swapping keys manually! 🚀