DEV Community

Cover image for Managing Multiple Git/GitHub Accounts with SSH
Gaurav J
Gaurav J

Posted on • Updated on

Managing Multiple Git/GitHub Accounts with SSH

Managing multiple GitHub Git accounts can be a challenge, especially when it comes to handling authentication and access. However, using SSH keys and configuring your SSH setup can streamline the process and make it more convenient. In this post, we'll explore the steps to handle multiple GitHub Git accounts using SSH.

Step 1: Generate SSH keys for each GitHub account
To begin, generate SSH keys for each GitHub account you want to manage. Open your terminal or command prompt and run the following command, replacing "your_email@example.com" with the email address associated with your GitHub account:

ssh-keygen -t ed25519 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

Generate SSH key

Remember to provide a unique name for each SSH key, such as "id_ed25519_account1" or "id_ed25519_account2". You can also set a passphrase for additional security. In above screenshot, I've entered 'gh-bitwisebro' as name for my SSH key file.

Step 2: Add SSH keys to the SSH Agent for Convenient Authentication
To avoid entering the SSH key passphrase every time you authenticate with your GitHub accounts, we can leverage the SSH Agent. The SSH Agent is a program that securely holds your SSH keys, allowing for seamless authentication during your Git operations.

  • Open command prompt in the .ssh folder add the SSH keys to agent using: ssh-add id_ed25519_account1

Remember to replace id_ed25519_account1 with your filename! My filename is gh-bitwisebro so I'll execute following command:

ssh-add gh-bitwisebro

Adding SSH key to SSH Agent

Note: Some of you might face errors like ssh-add error connecting to agent while adding SSH keys to SSH Agent. In that case follow following steps:

  • Makesure you've openSSH Client and openSSH Server installed. (You can do it from optional features menu in windows)
  • Make sure your openSSH Authentication Agent is running. (In windows, you can do it from Services)

Step 3: Add SSH keys to your GitHub accounts

  • navigate to .ssh folder: cd C:\Users\Gaurav Jani/.ssh/gh-bitwisebro
  • Open this folder in VS Code: code .
  • Now copy the key from public file i.e. file that has .pub extension

Copy the public key

Log in to your GitHub account for the first account and navigate to "Settings". In the left sidebar, click on "SSH and GPG keys". Add a new SSH key by providing a descriptive title and pasting the contents of the corresponding .pub file generated in Step 1. Repeat this step for each GitHub account you want to associate with an SSH key.

Go to SSH and GPG key section in github

Add SSH key to Github

Step 4: Create or modify your SSH configuration
Next, create or modify your SSH configuration file. Open it in your preferred text editor. We've already opened that folder in VS Code.

Create a file called config in the folder, i.e. .ssh folder

Add the following lines for each GitHub account, replacing the placeholders with your own values:

# Account 1
Host github-account1
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_account1

# Account 2
Host github-account2
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_account2
Enter fullscreen mode Exit fullscreen mode

In my Case, I'll add something like this:

Example config file

Save and close the file.

Step 5: Test the SSH connections
To ensure that your SSH connections are properly configured, open your terminal and run the following command for each GitHub account:

ssh -T git@github-account1
Enter fullscreen mode Exit fullscreen mode

or

ssh -T git@github-account2
Enter fullscreen mode Exit fullscreen mode

If everything is set up correctly, you should see a message like: "Hi username! You've successfully authenticated...".

Testing Github SSH connection

Step 6: Configure your Git repositories
Lastly, configure your Git repositories to use the appropriate SSH URLs. For existing repositories, update their remote URL using the command:

git remote set-url origin git@github-account1:username/repository.git
Enter fullscreen mode Exit fullscreen mode

For new repositories, when cloning, use the SSH URL with the configured hostname:

git clone git@github-account2:username/new-repository.git
Enter fullscreen mode Exit fullscreen mode

Getting correct SSH URL for github repo

So I'll clone the above repo using:
git clone git@gh-bitwisebro:bitwisebro/Learn-Git.git

Refer the image:
Cloning git repo using SSH

Conclusion:
By following these steps, you can easily manage multiple GitHub Git accounts using SSH. This approach allows you to switch between accounts seamlessly and ensures secure authentication. With separate SSH keys and host aliases, you can streamline your Git workflows and maintain a clear separation between your different GitHub accounts.

If you found this guide helpful and want to stay connected, I invite you to connect with me on:

Feel free to reach out, ask questions, or explore more of my work on these platforms. Let's connect and continue the conversation!

Thank you for reading and happy coding!

Top comments (0)