Use multiple GitHub accounts on same system
Tags: git, github, beginners, webdev
Published: 2022-12-14 18:36:10

I was working with two different GitHub accounts one is my personal account and the other is my work account. I had these two accounts set up on different systems but recently my personal system had some connectivity issues so I had to use both of my GitHub accounts on my work system. I did some research on how I can achieve that. In this blog post, I will try to explain how we can solve the problem of using two GitHub accounts on the same machine.

## **Secure Shell Protocol(SSH)**

To use two different GitHub accounts on the same machine we are going to use ssh.

<aside>
💡 SSH or Secure Shell is a network communication protocol that enables two computers to communicate and share data. An inherent feature of ssh is that the communication between the two computers is encrypted meaning that it is suitable for use on insecure networks. You can read more about ssh [here](https://www.ucl.ac.uk/isd/what-ssh-and-how-do-i-use-it#:~:text=SSH%20or%20Secure%20Shell%20is,web%20pages)
</aside>

## Let’s Get Into It.

- **Create SSH Key**
    
    We will need to create an ssh key for all the accounts that we are going to use on the machine.
    
    To do that open the terminal and move into the ssh directory
    
    <aside>
    💡  `$ cd ~/.ssh`
    
    </aside>
    
    Now we need to generate an ssh key. The syntax for the generation of an ssh key is
    
    <aside>
    💡 `ssh-keygen -t rsa -C "email-address" -f "github-username"`
    
    </aside>
    
    - -C option stands for comment which helps identify the ssh key
    - -f option stands for file name which means that whatever name you pass after -f that name will be the name of our ssh key file inside the ssh directory.
    
    After understanding how we can create the ssh key let’s now create the ssh key for our two accounts
    
    ***Personal Account***
    
    <aside>
    💡 `ssh-keygen -t rsa -C "mypersonalemail@gmail.com" -f "personal"`
    
    </aside>
    
    ***Work Account***
    
    <aside>
    💡 `ssh-keygen -t rsa -C "myworkemail@gmail.com" -f "work"`
    
    </aside>
    
    After entering the command the terminal will ask for a passphrase, leave it empty and proceed.
    
    What this command will do is it will create two files one with `.pub` extension and one without any extension. In our case four files will be created in the ssh directory named `personal` , [`p](http://personal.pub)ersonal.pub` , `work` and `work.pub`
    
- **Add SSH keys to SSH Agent**

Our keys will not work until we add them in ssh-agent or they will ask for a paraphrase every time we the key. You can read more about it [here](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent).

let's add both of our key to ssh-agent

<aside>
💡 ssh-add -K ~/.ssh/work
ssh-add -K ~/.ssh/personal

</aside>

- **Add SSH key to GitHub**

Now to use these newly generated ssh keys we need to add them in our GitHub account. So the work key will go into our work GitHub account similarly our personal key will go into our Personal GitHub account.

- Now copy the key form the `.pub` extension file.
- Sign in to GitHub Account
- Goto **Settings** > **SSH and GPG keys** > **New SSH Key**
- Paste your copied public key and give it a Title of your choice.

![sshshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9wck9ym16zsr75762xe3.png)

- **Create a Config File and Make Host Entries**
    
    After adding the key to our GitHub account we need to create a config file in the .ssh directory and make the host entry.
    
    If the config file is not already created, create a new config file and paste the following data into it.
    
    ```
    # githubPersonal
    
    		Host personal github.com
    		HostName github.com
    		PreferredAuthentications publickey
    		IdentityFile ~/.ssh/personal
    
    # githubWork
    
    		Host work github.com
    		HostName github.com
    		PreferredAuthentications publickey
    		IdentityFile ~/.ssh/work
    ```
    
    Now everything is done the only thing that remains is to clone the repositories. To achieve that, copy the ssh URL of the repository that you want to clone and use the below command
    
    <aside>
    💡 `git clone git@{your-username}:{owner-user-name}/{the-repo-name}.git`
    
    </aside>
    
    if I want to copy a repo from my personal account the above command will look something like this
    
    <aside>
    💡 `git clone git@personal:Hat52/blood-donation.git`
    
    </aside>
    
    From now on, to ensure that our commits and pushes from each repository on the system use the correct GitHub user — we will have to configure **user.email** and **user.name** in every repository freshly cloned or existing before.
    
    To do this use the following commands.
    
    ```
         git config user.email "work@gmail.com"
         git config user.name "Hamza Ali"
    
         git config user.email "personal@gmail.com"
         git config user.name "Hamza Ali"
    
    ```
    
    Pick the correct pair for your repository accordingly.
    
    To push or pull to the correct account we need to add the remote origin to the project
    
    ```
         git remote add origin git@personal:{personal-username}/{personal-repo}
    
         git remote add origin git@work:{user-name}/{work-repo}
    
    ```
    
    Now you can use:
    
    ```
         git push
    
         git pull
    ```
    
    **`This approach can be used for more then two account.`**
    

That is it from this post if you have any question or suggestion please leave them in the comments.