# Using multiple Github accounts with SSH

When I started using Github, I used Github Desktop for sometime because that felt easy. Down the line, I learnt using `git` via CLI. And, I haven't touched the electron tool back ever. But, I was still using Github via `HTTP` vs `SSH`. 

I fell in love with `ssh` when I first discovered it. Once setup you can use ssh to work with multiple repositories under same account. The only downside is the initial setup that might feel complicated if you are using it for the very first time. However, it saves you from the pain of password/token based authentication (...painful).

If you haven't used `ssh` before, these two articles would help you set up pretty quick. 

1. [Generating a new SSH Key](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent)
2. [Adding the Generated Key to your Github Account](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account)


So far so good, this works good for a single account. What if you have to use multiple accounts? Personal and Work? 

1. Use `ssh-keygen` to start creating your new ssh key. Note, this email should be same as the one you use with your Github account. 
    ```bash
        ssh-keygen -t rsa -C "youremail@domain.com"
     ```
2. Give this ssh-key a different name, so that it does not conflict/overwrite existing keys. All existing `ssh` keys can be found under `~/.ssh` folder. I named my own as "pavittarxnoauth".

   ```bash
      Enter file in which to save the key (/home/pavittarx/.ssh/id_rsa): pavittarxnoauth    
   ```

3. In the next steps, provide a password or simply hit enter if you do not want to add any passwords while authentication.

4. Check if the key has been successfully created. If you cannot find the key in .ssh folder. Repeat steps 1-3.
    ```bash
      ls ~/.ssh
    ``` 

5. Start your ssh-agent if you have not already. 
    ```bash
      eval `ssh-agent -s`
    ```

6. Add the key to your ssh agent. 

    ```bash
      ssh-add ~/.ssh/pavittarxnoauth
    ```
7. Navigate to `~/.ssh` directory.

    ```
      cd ~/.ssh
    ```

8. Add following entry to the `config` file. You can also create one if it doesn't exist. Replace, pavittarxnoauth with your key-file name. Also, replace `github-noauth` to anything as per your preference. 

   ```vim
  Host github-noauth
    HostName github.com
    User git
    IdentityFile ~/.ssh/pavittarxnoauth
   ```

9. Add the `~/.ssh/<yourkeyfilename.pub>` to your Github account as stated [here](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account).

10. Clone your repo with ssh, or change the `git remote` and `git config` for the repository. 

    ```bash
        git clone git@github-noauth:<your-github-username>/<your-git-repo>
    ```

    For existing repository, that has been cloned already.

    1. Change `git remote origin`

    ```bash 
      git remote remove origin
      git remote add origin git@github-noauth:<github-username>/<git-repo>
    ```
    2. Change `git config`
    
    ```bash
      git config --local user.email "your-github-email@domain.com"
      git config --local user.name "github-username"
    ```

You can use same process to add multiple keys, with same or different accounts to Github. Also, the same is reproducable for other git providers such as Bitbucket/ Gitlab.
