Managing Multiple Git/GitHub Accounts with SSH
Tags: github, git, cli, linux
Published: 2023-07-12 19:30:07

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"
```


![Generate SSH key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dxmph9n7pri4g7dh4ua1.png)



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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q3zscqd16aeymk52t2zq.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z8nf4gu7drdn9i253v2r.png)




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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n9hwj2kp6jw6f03b61ss.png)


![Add SSH key to Github](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rlyfgzcg5etvwacg3q0i.png)



**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
```
In my Case, I'll add something like this:

![Example config file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m53f6oatoaplnnu1nl7i.png)


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
```

or

```
ssh -T git@github-account2
```

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


![Testing Github SSH connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cn1kli3vds0b0bp48nax.png)



**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
```

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

```
git clone git@github-account2:username/new-repository.git
```


![Getting correct SSH URL for github repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/opgj70nedfio3hxz2vgz.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kkfnfd6xlljy29ernka7.png)



**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:

- LinkedIn: https://www.linkedin.com/in/bitwisebro/
- GitHub: https://github.com/bitwisebro

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!