All articles
git

How to Manage Multiple Git Accounts (GitHub, GitLab, Bitbucket) with SSH

Share this article

Share on LinkedIn Share on X (formerly Twitter)

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_dev

Step 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/config

Add 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_dev

Step 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-username

Using 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.git

Updating 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

  1. Git User Config: SSH handles authentication, but Git still needs to know who you are (user.name and user.email). Ensure these are set correctly per repository (see our guide on includeIf for automation).
  2. Key Permissions: Ensure your private keys are not readable by others: chmod 600 ~/.ssh/id_*.

Pro Tips

  • Use descriptive aliases like gh-personal or gl-work to save typing.
  • If you encounter "Permission denied (publickey)", use ssh-add -l to 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! 🚀


Comments