Back to blog
Nov 20, 2024
4 min read

How to Configure Multiple Git Accounts with SSH: A Complete Guide for Developers

Learn how to securely manage multiple Git accounts (GitHub, GitLab, Bitbucket) on your local machine using SSH keys.

Managing multiple Git accounts is a common challenge for developers who work with different organizations or maintain separate personal and professional repositories. Whether you’re juggling accounts across GitHub, GitLab, and Bitbucket, or managing multiple accounts on the same platform, this guide will show you how to set up and organize your Git accounts using SSH authentication.

Prerequisites

Before starting, ensure you have:

  • Git installed on your local machine
  • Basic understanding of Git commands
  • Access to terminal/command prompt
  • Accounts on your preferred Git platforms (GitHub, GitLab, Bitbucket)

Step-by-Step Configuration Guide

1. Generating SSH Keys

Create unique SSH keys for each Git account using this standardized naming convention: platformname_organizationname_rsa. The SSH key generation process follows these best practices:

# For GitHub Personal Account
ssh-keygen -t rsa -b 4096 -C "personal@email.com" -f ~/.ssh/github_personal_rsa

# For GitHub Work Account
ssh-keygen -t rsa -b 4096 -C "work@company.com" -f ~/.ssh/github_work_rsa

# For GitLab Account
ssh-keygen -t rsa -b 4096 -C "work@company.com" -f ~/.ssh/gitlab_work_rsa

# For Bitbucket Account
ssh-keygen -t rsa -b 4096 -C "work@company.com" -f ~/.ssh/bitbucket_work_rsa

2. Adding Keys to SSH Agent

Initialize the SSH agent and add your keys:

# Start the SSH agent
eval "$(ssh-agent -s)"

# Add your SSH keys
ssh-add ~/.ssh/github_personal_rsa
ssh-add ~/.ssh/github_work_rsa
ssh-add ~/.ssh/gitlab_work_rsa
ssh-add ~/.ssh/bitbucket_work_rsa

3. Adding SSH Keys to Git Platforms

Copy your public keys using:

# Use .pub extension to get the public key
cat ~/.ssh/github_personal_rsa.pub
cat ~/.ssh/github_work_rsa.pub
cat ~/.ssh/gitlab_work_rsa.pub
cat ~/.ssh/bitbucket_work_rsa.pub

Add these keys to their respective platforms:

4. Configuring SSH Config File

Create or edit ~/.ssh/config file with this configuration, following SSH config best practices:

# Personal GitHub
Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/github_personal_rsa

# Work GitHub
Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/github_work_rsa

# Work GitLab
Host gitlab-work
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/gitlab_work_rsa

# Work Bitbucket
Host bitbucket-work
    HostName bitbucket.org
    User git
    IdentityFile ~/.ssh/bitbucket_work_rsa

5. Repository Management

Cloning Repositories

# Personal GitHub
git clone git@github-personal:username/repo.git

# Work GitHub
git clone git@github-work:organization/repo.git

# Work GitLab
git clone git@gitlab-work:organization/repo.git

# Work Bitbucket
git clone git@bitbucket-work:organization/repo.git

Setting Remote URLs

# Personal GitHub
git remote set-url origin git@github-personal:username/repo.git

# Work GitHub
git remote set-url origin git@github-work:organization/repo.git

# Work GitLab
git remote set-url origin git@gitlab-work:organization/repo.git

# Work Bitbucket
git remote set-url origin git@bitbucket-work:organization/repo.git

Troubleshooting Common Issues

  1. Authentication Failed

    • Verify SSH key is added to SSH agent
    • Check if public key is properly added to Git platform
    • Ensure correct host configuration in SSH config file
    • Refer to GitHub Troubleshooting SSH Issues
  2. Wrong Account Used

    • Confirm remote URL matches desired account
    • Verify SSH config host matches remote URL
    • Check current Git global configuration
    • Use Git Identity Management best practices

Best Practices and Security Tips

  1. Key Management

    • Use descriptive key names
    • Maintain separate keys for each account
    • Regularly rotate SSH keys (recommended: every 6-12 months)
    • Follow SSH Key Security Guidelines
  2. Configuration

    • Keep SSH config file organized and documented
    • Use clear, consistent naming conventions
    • Regularly backup SSH keys and configurations
    • Understand SSH Config File Syntax
  3. Security

    • Use strong passphrases for SSH keys
    • Store private keys securely
    • Never share private keys
    • Use ed25519 keys for better security when possible
    • Implement Two-Factor Authentication on Git platforms

This guide provides a robust foundation for managing multiple Git accounts securely and efficiently. By following these steps and best practices, you can maintain separate Git identities while ensuring smooth workflow across different platforms and organizations.

Disclaimer: This guide is for informational purposes and should be adapted to your specific security requirements.