~/blog/git-ssh-setup-multiple-accounts
Published on

Git - Effective Guide to Managing Multiple SSH Keys for Different GitHub Accounts

790 words4 min read–––
Views
Authors
  • avatar
    Name
    Saikrishna Reddy
    Instagram

Imagine you have two Git accounts – a personal one and an office account. When working on different projects, you need to ensure that Git uses the appropriate account for committing and pushing changes. To achieve this, I'll guide you through setting up SSH keys for both accounts.

First, create two distinct folders: one called "work" for office-related projects and another named "personal" for your personal endeavors. All your office projects will be housed within the "work" folder, while your personal projects will reside in the "personal" folder.

With this smart setup, whenever you perform any Git operation from within the "work" folder, Git will automatically utilize your office account. Similarly, when working from the "personal" folder, Git will employ your personal Git account for all committing and pushing actions. This simple yet effective approach ensures a seamless workflow between your two Git accounts based on your project locations. Let's get started with the setup!

  1. Generate SSH Key Pair for Personal Account: Open a terminal or command prompt and run the following command:

    ssh-keygen -t ed25519 -C "personal@example.com"
    

    This command generates a new SSH key pair using the Ed25519 algorithm. It will prompt you for a file location and passphrase. You can press Enter to use the default location (usually ~/.ssh/id_ed25519) and leave the passphrase empty if you prefer. Make sure to replace "personal@example.com" with your personal email.

  2. Generate SSH Key Pair for Office Account: Repeat the previous step to generate another SSH key pair for your office account:

    ssh-keygen -t ed25519 -C "office@example.com"
    

    Choose a different file location and passphrase for this key. Again, replace "office@example.com" with your office email.

  3. Configure .gitconfig for Conditional Includes: Open or create your global .gitconfig file (~/.gitconfig) and add the following:

    [includeIf "gitdir:~/work/"]
      path = ~/.gitconfig-office
    
    [includeIf "gitdir:~/personal/"]
      path = ~/.gitconfig-personal
    

    The above configuration specifies that if you are in a directory that contains work in its path, Git will include the settings from ~/.gitconfig-office. Similarly, if you are in a directory that contains personal in its path, it will include the settings from ~/.gitconfig-personal.

  4. Create Separate Git Config Files: Now, create two separate Git config files for your office and personal accounts.

    • Create ~/.gitconfig-office:
    [user]
      email = office@example.com
      name = Your Office Name
    [core]
      sshCommand = "ssh -i ~/.ssh/office_id_ed25519"
    
    • Create ~/.gitconfig-personal:
    [user]
      email = personal@example.com
      name = Your Personal Name
    [core]
      sshCommand = "ssh -i ~/.ssh/personal_id_ed25519"
    

    In the above configurations, we set different email and name values based on the account and specify the correct SSH key (office_id_ed25519 or personal_id_ed25519) using the sshCommand option.

  5. Add SSH Keys to the Appropriate SSH Agent: Use an SSH agent to manage your SSH keys. You can start the agent and add your keys as follows:

    # Start the SSH agent (if not already running)
    eval "$(ssh-agent -s)"
    
    # Add the personal SSH key to the agent
    ssh-add ~/.ssh/personal_id_ed25519
    
    # Add the office SSH key to the agent
    ssh-add ~/.ssh/office_id_ed25519
    

    Make sure to replace personal_id_ed25519 and office_id_ed25519 with the actual filenames you used during key generation.

  6. Add SSH Public Keys to Git Hosting Providers: Copy the contents of both public keys (personal_id_ed25519.pub and office_id_ed25519.pub) using the following commands:

    # Personal public key
    cat ~/.ssh/personal_id_ed25519.pub
    
    # Office public key
    cat ~/.ssh/office_id_ed25519.pub
    

    Log in to your personal GitHub account and add the public key under "Settings" -> "SSH and GPG keys". Do the same for your office GitLab account or any other Git hosting provider.

Now, you have set up SSH key-based authentication for both personal and office accounts, and Git will automatically use the appropriate SSH key and configuration based on the directory you are working in. When you push or pull from personal projects, the personal_id_ed25519 key and settings from ~/.gitconfig-personal will be used. For office projects, the office_id_ed25519 key and settings from ~/.gitconfig-office will be used. The SSH agent will manage the keys and handle the authentication process automatically.

NOTE : Clone the repos again using ssh and start working.

This setup can be applied to any scenario where you need to configure Git to use different Git accounts for various Git operations. Whether it's managing personal and work-related projects or any other use case, this approach allows you to seamlessly switch between multiple Git accounts based on your specific project requirements.