# How to Set Up Multiple Git Accounts on the Same Machine Using SSH Keys.

Hello everyone! I'm Ashish Maurya, a full-time software developer and part-time freelancer. With the different projects I work on for different clients, I often have to set up work emails with Git. Unfortunately, it can be tricky to set up multiple Git accounts on the same machine. In this article, I'll show you how to use multiple Git accounts on the same machine using SSH keys.

### The Problem 😒

Well setting up one git account is easy setting up multiple accounts is not so much especially when you have to keep switching between the different git accounts. (As I mentioned part-time freelance developer)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676491092855/6102937e-fcab-4d92-8b02-fe05373e6e16.gif align="center")

### Solution 👌

There is a way to set up multiple accounts in Git on the same machine using `ssh` keys. This article will show how to set up numerous git accounts on the same Machine.

## Setting Up the SSH keys

### Generating `SSH` keys.

* To generate SSH keys just open your favorite terminal or you can open Git bash and write the following command.
    

```bash
$ ssh-keygen -t ed25519 -C "your_email@example.com"
```

* Once that is done, you will be prompted to enter a file name. You can name it anything but in case you already have `.ssh` keys, don't name it the same or else it will be overwritten.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676824877839/07c8a2e7-26b8-4d40-910a-724625707a5d.png align="center")

For this article, I will use `test` it as a dummy username but you can name it anything you like. In case you want to check for existing keys follow this [Link](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/checking-for-existing-ssh-keys).

* After this, you will be asked to enter a passphrase. You can read more about the passphrase [here](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/working-with-ssh-key-passphrases).
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676825067064/0d10e611-286c-4dfe-a467-63b2590d844d.png align="center")

> With SSH keys, if someone gains access to your computer, the attacker can gain access to every system that uses that key. To add an extra layer of security, you can add a passphrase to your SSH key. To avoid entering the passphrase every time you connect, you can securely save your passphrase in the SSH agent.

* You will see a key fingerprint will be generated.
    

### Adding `SSH` key

* Once you are done with the above steps you will be needing to add the SSH key. For that, you can run the following command.
    

```bash
$ eval "$(ssh-agent -s)"
>> Agent pid 375
```

* Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace the *test* in the command with the name of your private key file.
    

```bash
$ ssh-add ~/.ssh/test
Identity added: /c/Users/Lenovo/.ssh/test (your_email@example.com)
```

### Adding the above `SSH` key to GitHub

* Copy the above SSH public key to your clipboard.
    
    ```bash
    $ clip < ~/.ssh/test.pub
      # Copies the contents of the test.pub file to your clipboard
    ```
    
* Open the setting in your GitHub account.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676826649834/e7b9ff0a-1ac1-4f71-a076-75b779b9b313.png align="center")

* Open the SSH key & GPG keys from the sidebar.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676826827600/02c0a9e4-cf0b-4b1a-b213-1381822260a4.png align="center")

* Click on add New Key and a prompt will open in that text area paste the keys and Add them.
    
* Once that is added you have added the key Successfully.
    

## Adding more Accounts and Managing them.

### Adding Account

Now repeat the same process for your work account or personal account. And once done.

### Maintaining multiple Accounts

1. Edit/Create ssh config file (`~/.ssh/config`):
    
    ```bash
    # Default github account: personal
    Host github.com
       HostName github.com
       IdentityFile ~/.ssh/personal # your personal SSH key
       IdentitiesOnly yes
       
    # Other github account: test
    Host github-test
       HostName github.com
       IdentityFile ~/.ssh/test #Your test/ Organization key
       IdentitiesOnly yes
    ```
    
2. Adding SSH private key to your agent.
    
    ```bash
    $ ssh-add ~/.ssh/personal
    $ ssh-add ~/.ssh/test
    ```
    
3. Test the connection.
    
    ```bash
    $ ssh -T git@github.com
    $ ssh -T git@github-test
    ```
    
    you will get the following prompt with each command, you might see this kind of warning type `yes` in each case.
    
    ```bash
    The authenticity of host 'github.com (192.30.252.1)' can't be established.
    RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:
    Are you sure you want to continue connecting (yes/no)?
    ```
    
4. If everything is OK, you will see these messages:
    
    ```yaml
    Hi personal! You've successfully authenticated, but GitHub does not provide shell access.
    ```
    
    ```yaml
    Hi test! You've successfully authenticated, but GitHub does not provide shell access.
    ```
    
5. Now all are set, just clone your repositories
    
    ```bash
    $ git clone git@github-test:org2/project2.git /path/to/project2
    $ cd /path/to/project2
    $ git config user.email "test@org2.com"
    $ git config user.name  "Test"
    ```
    

### Conclusion

In conclusion, setting up multiple Git accounts on the same machine can be tricky, but it is possible with the use of SSH keys. By following the steps outlined in this article, you can easily manage multiple Git accounts from the same machine. With SSH keys, you can securely access multiple accounts without having to constantly enter and store passwords.

### References

https://github.com  
[https://gist.github.com/oanhnn/80a89405ab9023894df7](https://gist.github.com/oanhnn/80a89405ab9023894df7)  
[https://www.freecodecamp.org/news/manage-multiple-github-accounts-the-ssh-way-2dadc30ccaca/](https://www.freecodecamp.org/news/manage-multiple-github-accounts-the-ssh-way-2dadc30ccaca/)
