How to work with multiple GitHub accounts
Tags: tutorial, opensource, github, productivity
Published: 2023-03-24 23:16:37

Is it possible to have multiple GitHub accounts that you can use for different purposes? Yes, it is, and you can do so easily with as many GitHub accounts as possible.

I found it challenging when I created another account on GitHub and discovered it was not possible to use the same [git configuration](https://git-scm.com/) for the first account on setup without doing something extra to talk with the new GitHub account.

My primary GitHub account is [https://github.com/terieyenike](https://github.com/terieyenike), and the second account is [https://github.com/developedbyteri](https://github.com/developedbyteri). 


> Note: To create another account with the same email address, use plus(“+”) sign like this: `name+anyword@gmail.com`


## Prerequisites

The following setup is required to follow through:

- Have git on your local machine. [Download it](https://git-scm.com/) for your operating system (OS)
- You already have an account on GitHub. [Sign-up](https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F&source=header-home) is free
- Terminal


![GitHub](https://paper-attachments.dropboxusercontent.com/s_8CDCEED1CA6A04B84DFC2173CAE2F002A528950CACE4AC14DCF961048D592FB4_1679658152090_Screenshot+2023-03-24+at+12.42.25.jpg)


**Note**: I will work on macOS for all the actions in this tutorial.

Let’s get started with these steps:


## Step 1

**Create secure shell (SSH) keys**
Make sure the `.ssh` directory is present in your home directory with this command:

```bash
    cd ~/.ssh
```

Writing this command in your terminal, learn [how to use bash](https://hackernoon.com/how-to-use-bash).

To check you have this hidden folder, type:

```bash
    ls -a
```

This command above lists all the folders present on your system.

If otherwise, create it with this command:

```bash
    touch .ssh
```

Next, generate a unique ssh key for this account: 

```bash
    ssh-keygen -C "<email-address>" -t rsa -f "<name-of-file>"
```

After pressing the enter key, the terminal will ask for a passphrase; you can leave it empty and accept the defaults.

`ssh-keygen`: a tool for creating the authentication key pair for SSH
`-C`: represent comment to identify the ssh key
`-t`: is the type of key created using [rsa](https://en.wikipedia.org/wiki/RSA_(cryptosystem))
`-f`: name of the file for storing the keys
`<email-address>`: The email address for your GitHub account
`<name-of-file>`: use any name of your choice

Note: change the placeholders in the **<>** symbol

The command will generate the public and private keys. The public key will have an extension `.pub`, and the private key will be the same name without the extension. The private key is not to be shared and kept secret.

Use this command, `ls -l`, to view the generated keys.


![public and private key](https://paper-attachments.dropboxusercontent.com/s_8CDCEED1CA6A04B84DFC2173CAE2F002A528950CACE4AC14DCF961048D592FB4_1679666977978_Screenshot+2023-03-24+at+15.09.33.jpg)



## Step 2

**Add SSH keys to the SSH agent**
Before using the keys, you will need to add the private key to the SSH agent in the terminal:

```bash
    ssh-add ~/.ssh/developedbyteri
```

## Step 3

**Add SSH key to your account**
In this section, you will add the generated public key pair to your GitHub account. Use this command.

**Copy the public key**

```bash
    pbcopy < developedbyteri.pub
```

This command will copy the **public key** to the clipboard. 



![public key](https://paper-attachments.dropboxusercontent.com/s_8CDCEED1CA6A04B84DFC2173CAE2F002A528950CACE4AC14DCF961048D592FB4_1679667714806_Screenshot+2023-03-24+at+15.21.01.jpg)


OR

You can choose to use either vim or nano keyword to reveal the public key and copy it:

```bash
    vim ~/.ssh/developedbyteri.pub
    
    nano ~/.ssh/developedbyteri.pub
```

**Paste the public key to GitHub**

- Sign in to your GitHub account
- Click on your profile in the upper right corner of the page and select **Settings**
- Select **SSH and GPG keys** and create a **New SSH key,** respectively
- Paste the copied public key, not the private key, and give the key a title


![ssh key github](https://paper-attachments.dropboxusercontent.com/s_8CDCEED1CA6A04B84DFC2173CAE2F002A528950CACE4AC14DCF961048D592FB4_1679668476869_Screenshot+2023-03-24+at+15.33.04.jpg)


## Step 4

**Modify the config file**
Create a config file.

But first, if the file doesn’t exist, use this command to create one in the **~/.ssh** directory:

```bash
    touch config
```

Use this command to open the file in your default text editor (TextEdit, VS Code): 

```bash
    open config
```

If you want to use VS Code to open this file, use the command:

```bash
    code config
```

Now, copy-paste this:

```bash
# terieyenike account
Host github.com-terieyenike
        Hostname github.com
        IdentityFile=~/.ssh/id_rsa

#developedbyteri account
Host github.com-developedbyteri
        Hostname github.com
        IdentityFile=~/.ssh/developedbyteri
```

- `Host github.com-terieyenike`: The repository's alias. Change the value after the **-** to your desired alias
- `Hostname github.com`: Configures the host name, **github.com** to use the alias
- `IdentityFile=~/.ssh/id_rsa`: The path to the private key alias

**Note**: Change the values like **id_rsa**, **developedbyteri**, the alias **-terieyenike** or **-developedbyteri** to the naming convention you choose or present in the **~/.ssh** folder respectively.


## Step 5

**Fork and** **Clone a repository**
With all the setup done from the previous steps, let’s fork and clone a repository using the newly created GitHub account, which differs from your main account.

In this section, you will contribute to an open-source project from this repository, [https://github.com/Terieyenike/cloudinary-upload](https://github.com/Terieyenike/cloudinary-upload).

**Fork this repository**
Open the link above, and click on the **fork** button to create an entirely new copy of a repository in your account.


![fork a repo](https://paper-attachments.dropboxusercontent.com/s_8CDCEED1CA6A04B84DFC2173CAE2F002A528950CACE4AC14DCF961048D592FB4_1679674351866_Screenshot+2023-03-24+at+17.11.53.jpg)


After that, click the **Create fork** button. You can always change the **Repository name** or the **Description** if you desire.


![create fork](https://paper-attachments.dropboxusercontent.com/s_8CDCEED1CA6A04B84DFC2173CAE2F002A528950CACE4AC14DCF961048D592FB4_1679674421997_image.png)


Next, click the green **Code** button dropdown and copy the URL using HTTPS, SSH protocols or the GitHub CLI. Choose one.

I will be using the SSH protocol. 

**Cloning**
Cloning creates a local copy of the repository on your local computer. Check out the [official docs](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository) of GitHub on cloning.

Back to your terminal, clone the repository to a desired location on your local machine using this command:

```bash
    git clone git@github.com-alias:{username}/{repository-name}.git
    
    git clone git@github.com-developedbyteri:developedbyteri/cloudinary-upload.git
```

![clone repo](https://paper-attachments.dropboxusercontent.com/s_8CDCEED1CA6A04B84DFC2173CAE2F002A528950CACE4AC14DCF961048D592FB4_1679675244638_Screenshot+2023-03-24+at+17.26.03.jpg)



## Step 6

Let’s work on the cloned repository by navigating to the directory with this command:

```bash
    cd cloudinary-upload
```

Open the folder in VS Code:

```bash
    code .
```

In the folder, click on the **README.md** file. I will edit this file just for an example.

**Stage and commit the file**
Use this command below to stage and commit the file locally. 

**Note**: You can stage the name of the single file you changed or the entire project using the command `git add .`

```bash
    git add README.md
    
    git commit -m "Add: include the name of the hackathon"
```

**-m:**  adds a commit message for the changes made to the file


## Final step

Before pushing this code back to GitHub, let’s configure this directory with the user email and name to track its changes on this account.

To do this, use the following commands:

```bash
    git config user.email "name+github@gmail.com"
    git config user.name "Codegod" 

    git config user.email "name@gmail.com"  
    git config user.name "Teri Eyenike"
```

- `user.email`: GitHub account email when creating an account
- `user.name`: The name of your GitHub account

**Note**: Always remember to use this command for different accounts from the primary one.

**Push the code**
This command will push the code to the remote repository.

```bash
    git push
```

Go back to your account. The repository will update with the message “This branch is 1 commit ahead …”; click on it.


![1 commit ahead](https://paper-attachments.dropboxusercontent.com/s_8CDCEED1CA6A04B84DFC2173CAE2F002A528950CACE4AC14DCF961048D592FB4_1679678864918_Screenshot+2023-03-24+at+18.26.19.jpg)


With the file open, click the green button, **Create pull request** and inspect this file. You should see the highlighted message in green you added earlier in VS Code.


![create pull request](https://paper-attachments.dropboxusercontent.com/s_8CDCEED1CA6A04B84DFC2173CAE2F002A528950CACE4AC14DCF961048D592FB4_1679678979546_Screenshot+2023-03-24+at+18.29.00.jpg)


Next, click the **Create pull request** button, and it opens to check the branch for conflicts. Once it passes, it shows a green check mark to signify everything is okay with your changes.


![pull request passed](https://paper-attachments.dropboxusercontent.com/s_8CDCEED1CA6A04B84DFC2173CAE2F002A528950CACE4AC14DCF961048D592FB4_1679679429250_Screenshot+2023-03-24+at+18.36.42.jpg)

## Create a new repository
If you create a new repository from **GitHub.com**, you have to add the repository alias like this:

```bash
git remote add origin git@github.com-terieyenike:Terieyenike/Tech-Writing-Resources.git

git remote add origin git@github.com-developedbyteri:developedbyteri/Tech-Writing-Resources.git
```

After that, repeat the instruction in the **Final step** and push the code to the remote origin.

```bash
git push -u origin main
```


## Conclusion

Now that you know, you can have as many GitHub accounts as possible and use these steps to add, update, and modify code in any GitHub accounts you choose without any issues.

If you found this article helpful, share it with someone who might benefit, as I struggled with it until I found a hack and solution.


## Further reading
- [SSH agent](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent)
- [Git configuration](https://git-scm.com/)
- [Using multiple repositories in one server](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/managing-deploy-keys#using-multiple-repositories-on-one-server)