DEV Community

Cover image for How to work with multiple GitHub accounts
oteri
oteri

Posted on • Edited on

How to work with multiple GitHub accounts

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 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, and the second account is 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 for your operating system (OS)
  • You already have an account on GitHub. Sign-up is free
  • Terminal

GitHub

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:

    cd ~/.ssh
Enter fullscreen mode Exit fullscreen mode

Writing this command in your terminal, learn how to use bash.

To check you have this hidden folder, type:

    ls -a
Enter fullscreen mode Exit fullscreen mode

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

If otherwise, create it with this command:

    touch .ssh
Enter fullscreen mode Exit fullscreen mode

Next, generate a unique ssh key for this account:

    ssh-keygen -C "<email-address>" -t rsa -f "<name-of-file>"
Enter fullscreen mode Exit fullscreen mode

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

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:

    ssh-add ~/.ssh/developedbyteri
Enter fullscreen mode Exit fullscreen mode

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

    pbcopy < developedbyteri.pub
Enter fullscreen mode Exit fullscreen mode

This command will copy the public key to the clipboard.

public key

OR

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

    vim ~/.ssh/developedbyteri.pub

    nano ~/.ssh/developedbyteri.pub
Enter fullscreen mode Exit fullscreen mode

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

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:

    touch config
Enter fullscreen mode Exit fullscreen mode

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

    open config
Enter fullscreen mode Exit fullscreen mode

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

    code config
Enter fullscreen mode Exit fullscreen mode

Now, copy-paste this:

# 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
Enter fullscreen mode Exit fullscreen mode
  • 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.

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

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

create fork

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 of GitHub on cloning.

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

    git clone git@github.com-alias:{username}/{repository-name}.git

    git clone git@github.com-developedbyteri:developedbyteri/cloudinary-upload.git
Enter fullscreen mode Exit fullscreen mode

clone repo

Step 6

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

    cd cloudinary-upload
Enter fullscreen mode Exit fullscreen mode

Open the folder in VS Code:

    code .
Enter fullscreen mode Exit fullscreen mode

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 .

    git add README.md

    git commit -m "Add: include the name of the hackathon"
Enter fullscreen mode Exit fullscreen mode

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

    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"
Enter fullscreen mode Exit fullscreen mode
  • 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.

    git push
Enter fullscreen mode Exit fullscreen mode

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

1 commit ahead

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

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

Create a new repository

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

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
Enter fullscreen mode Exit fullscreen mode

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

git push -u origin main
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)