Managing Multiple Git & GitHub Account

Tonmoy DebTonmoy Deb
7 min read

Introduction

As a developer, for work purposes, we always need to work with multiple github accounts at once. If you have multiple systems then there is no problem with it but If you want to manage multiple github in one system then today's article is for you.

What is the problem?

The first problem comes when you need to access different repositories from different github accounts. Whenever you need to clone a repository or push any changes, you need to authenticate each account on your terminal, again and again.

The second problem is you need to commit codes with different accounts also. So the same thing again, you need to switch between different accounts again & again.

Now as you can see switching stuff again & again is a hassle.

What is the solution?

To solve the first problem we need to use SSH key-based authentication other than the regular traditional Personal access tokens way. While cloning a private repository we need to use the SSH method instead of the HTTPS URL method.

To solve the second problem we need to use folder-based git config. Where a git config will apply to a specific folder only.

Multiple Github account using SSH

Before getting started with some commands you need to make sure ssh-keygen is installed in your system. In most of the Linux operating systems, it’s already installed by default, and for Windows, you need to enable Open SSH Client from optional features.

If everything is set correctly then let’s get started

For example, I’ve two emails user1@email.com and user2@email.com. Both accounts have separate github accounts also. Now I would like to authenticate both accounts on my machine.

Step 1: Generate SSH Key

Execute this command in your terminal to generate an SSH key for user1@email.com but make sure you have a .ssh folder in your home directory. If not then create one.

ssh-keygen -t rsa -C "user1@email.com" -f ~/.ssh/user1

Explanation

  • ssh-keygen is The command to generate an SSH key.

  • t rsa Specifies the key type as RSA (Rivest-Shamir-Adleman), a widely used public-key cryptosystem.

  • C "user1@email.com" Adds a comment (usually an email) to help identify the key.

  • f ~/.ssh/user1 Specifies the file name and location for storing the key:

    • ~/.ssh/ is the default SSH directory inside the user's home folder.

    • user1 is the custom filename for the private key (instead of the default id_rsa).

What happens after running this command?

  1. You might be prompted to set a passphrase and to confirm the passphrase for added security. You can just press enter and skip them.

  2. If everything goes correctly then it will generate two files:

    • ~/.ssh/user1 → The private key (keep this secret).

    • ~/.ssh/user1.pub → The public key (sharable with github).

Step 2: Registering SSH Key

In this step, we will register our generated SSH public key to github. To do that first we need to get the public key.

cat ~/.ssh/user1.pub

Explanation:

  • cat Reads and displays the contents of a file.

  • ~/.ssh/user1.pub This is the public key file generated earlier with ssh-keygen.

What happens after running this command?

After executing this command you will get something like this in the terminal.

ssh-rsa AAAAB3NzaC1yc2EDDDDDAQABAAABgQDRaOTJKD8oa7ZKdCYoiE9fXjR9XFWL8mejeBLRt7KKLhVBshl5IZPu4cwkzpe+ALbjDP2QLOKeNP13jfTqH+mDqTXQUOswtOOgdyQiTWn6w4dyW4C0W4HN5gwkSsqqWpF9jBp9fkkL15gB9yas8VHFI9uyhAHgSYzi4RwOtSYN/duK5ZtiT4qSSivxLfMbRt8LHBHKs+IdkmT5EdtDDEVsSWR4RutjOvAdQbdakMQT+z/5C/JijS9p8XdqWi2J55owDHP2DLHWpYxL5CqZW/ulf/LFMeQmF35L7HqNBODY/x5zWCiIAtzngltppL5yoecxAWTER8nzU0OnwdNTj+QCLemKcPgpAif/kZd0MO9/1lYdOt6AS/hXb8f7IRRqLzS9js2TFvUXGIYGQtrbYKq0jIWowauQryt6caPkvRLZBUWVIfpgU4Xii1Hmh7crwospOvKUJZjXtXaygHa+U/abV+yUWitBk7+cA6BSHgdkuZCE0YBZ4eYOeYPpdluCKrk= user1@email.com

Copy that key and go to this **SSH & GPG keys** github **page using user1@email.com and click on the **New SSH Key button. Now give this key a title (maybe your device name), the key type will be Authentication Key, and paste the earlier copied public key content into the key field. After that click on Add SHH Key button and save the key.

Step 3: SSH Config

We have successfully authenticated our device with Github. Now, the last thing we need is a config file where we will write all the rules to ensure that the correct SSH key is used for authentication. So create a file called config in the ~/.ssh folder and put these lines in the file.

Host    github.com-user1
    HostName github.com
    User git
    IdentityFile ~/.ssh/user1

Explanation:

  • Host github.com-user1 This defines a shortcut (github.com-user1) for connecting to GitHub using user1's SSH key.

  • HostName github.com Specifies the actual hostname, which is github.com.

  • User git The user for SSH authentication is git. This is required when using SSH for GitHub.

  • IdentityFile ~/.ssh/user1 Specifies the SSH key to use (~/.ssh/user1) when connecting with this alias.

What does it do?

So basically when we clone a repository or push some changes to a repository using SSH URL, It looks like this.

git@github.com:user1/latebro.git

Here github.com is the remote host. Now this config file created a shortcut hostname called github.com-user1. Now if we use this then it will still connect to the github.com remote host but for authentication, it will use the user1 public key. So the URL will look like this.

git@github.com-user1:user1/latebro.git

And as I said you can now access any private repository of user1 . You just need to use this format in the SSH URL.

Now you can do the same steps for your all other accounts and you are done with setting up multiple github accounts on one device. In my case, I’ve two accounts so my SSH config file looks like this.

Host    github.com-user1
    HostName github.com
    User git
    IdentityFile ~/.ssh/user1

Host    github.com-user2
    HostName github.com
    User git
    IdentityFile ~/.ssh/user2

I think you already guessed that for user2 repositories I’m going to use an SSH URL like this.

git@github.com-user2:user2/webcamio.git

Folder-Based Git Profile

Now our authentication problem is solved & we can clone, and push changes using a specific formatted URL. But still, we have a problem that we can clone from different accounts but our commit is going from one profile.

To solve that we will use the Folder-Based Git profile technique. Here we will define a Git profile for a specific folder only. Let’s say I’ve two working folders called user1-projects and user2-projects . Now I want to have the user1 git profile in the user1-projects folder & user2 git profile in the user2-projects folder. So whenever I work on the user1-projects folder, My commit history will contain the user1 git profile & If I work on the user2-projects folder, then My commit history will contain the user2 git profile.

First of all, we need to create separate gitconfig files for each profile. In my case user1 & user2. To organize stuff better I’m creating them inside a folder called gitconfigs in the home directory.

# user1
[user]
    name = User One
    email = user1@example.com
# user2
[user]
    name = User Two
    email = user2@example.com

Now to set up everything & define the rules to use which config file when? We need to create an index config file in our home folder called .gitconfig. If you already worked with git then you most probably already have it. So just put these lines in your git config file.

[includeIf "gitdir:~/user1-projects/"]
    path = ~/gitconfigs/user1

[includeIf "gitdir:~/user2-projects/"]
    path = ~/gitconfigs/user2

What It Does?

This tells Git:

  • If I’m working on ~/user1-projects directory then use git config from ~/gitconfigs/user1

  • If I’m working on ~/user2-projects directory then use git config from ~/gitconfigs/user2

That’s it we are done with setting up the Folder-Based Git Profile. Now all you need to do is modify stuff as per your need and you're good to go for building projects.

Conclusion

Dealing with multiple GitHub accounts on one machine can be tiring, but with SSH-based authentication and folder-specific Git profiles, you can simplify the process significantly. The need for repetitive authentications when switching accounts is mitigated by configuring the custom SSH micro for each account. Moreover, it's possible to set up folder-based Git profiles so that commits are made to the appropriate GitHub accounts.

With this enabled, you are free to work on as many repositories as you desire without needing to authenticate, masticate, or switch between accounts to cloud this environment to blend or make the invites. This tactic helps keep the development environment untangled while optimizing time for other matters. Happy coding! 🚀

1
Subscribe to my newsletter

Read articles from Tonmoy Deb directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tonmoy Deb
Tonmoy Deb

Web app developer who loves clean, organized code. Passionate about building projects, solving problems, and sharing knowledge through insightful coding blogs.