Effective Strategies for Maintaining Multiple GitHub Accounts

Sheikh VazidSheikh Vazid
4 min read

Managing multiple GitHub accounts can be a hassle, especially when balancing personal, work, and client projects. This guide will help you streamline the process by automating commits and pushes.

Step 1: Organize Your Folders

To maintain multiple GitHub account in your system best way to do is create separate for each of them such as

mkdir -p ~/Personal
mkdir -p ~/Work
mkdir -p ~/client1

Here in each of folder we can create work in each folder and each folder can align to correct git credentials

Step 2: Create Config Files for Each Account

nano ~/.gitconfig-personal

Add the following content. Replace the email with your actual personal email.

[user]
    name = your-personal-github-username
    email = your-personal-email@example.com
nano ~/.gitconfig-work

Add the following content. Replace the email with your actual work email.

[user]
    name = your-work-github-username
    email = your-work-email@example.com
nano ~/.gitconfig-client-1

Add the following content. Replace the email with your actual client1 email.

[user]
    name = your-client1-github-username
    email = your-client1-email@example.com

Step 3: Update Your Main Git Config

Now, edit your main ~/.gitconfig file to automatically use the correct config file based on the project's path.

Open ~/.gitconfig and make it look like this. This sets your personal account as the default and creates rules for your work directory.

Usually when your are working on work laptop or system you should keep default as work username and email then Set your work account as the default on your work laptop to ensure commits use the correct credentials

# This is your default (work) user config
[user]
    name = your-work-github-username
    email = your-personal-email@example.com

# --- Automatic Profile Switching ---
# If the project is inside ~/dev/work/, use the work config
[includeIf "gitdir:~/Personal"]
    path = ~/.gitconfig-personal

[includeIf "gitdir:~/client1"]
    path = ~/.gitconfig-client1

With this setup, Git will automatically use the correct name and email for your commits.

Step 4: Manage SSH Keys for Authentication

To push and pull from different accounts on the same service (like GitHub), you need separate SSH keys.

1. Generate Two SSH Keys

Create a unique SSH key for each account.

# Personal key
ssh-keygen -t ed25519 -C "your-personal-email@example.com" -f ~/.ssh/id_ed25519_personal

# Work key
ssh-keygen -t ed25519 -C "your-work-email@example.com" -f ~/.ssh/id_ed25519_work

# Client key
ssh-keygen -t ed25519 -C "your-client1-email@example.com" -f ~/.ssh/id_ed25519_client1

If you’re thinking what that id_ed25519: This is the cryptographic algorithm used to generate the key. Ed25519 is a modern, secure, and fast choice for SSH keys.

When prompted for a passphrase, you can either enter one for extra security or press Enter to skip.

2. Add Keys to Your Accounts

Copy the contents of each public key (.pub file) and add them to your corresponding GitHub accounts in Settings > SSH and GPG keys.

Copy the contents of each public key (.pub file) and add them to your corresponding GitHub accounts in Settings > SSH and GPG keys.

# Copy your personal public key
pbcopy < ~/.ssh/id_ed25519_personal.pub

# Copy your work public key
pbcopy < ~/.ssh/id_ed25519_work.pub

# Copy your client public key
pbcopy < ~/.ssh/id_ed25519_client1.pub

3. Configure SSH to Use the Correct Key

Create or edit your SSH config file at ~/.ssh/config. This file tells your computer which key to use for which account.

nano ~/.ssh/config

Add the following configuration:

# Personal GitHub account
Host github.com-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_personal
    IdentitiesOnly yes

# Work GitHub account
Host github.com-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work
    IdentitiesOnly yes

# Client1 GitHub account
Host github.com-client1
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_client1
    IdentitiesOnly yes

4. Clone Repos Using Custom Hosts

From now on, when you clone a repository, you must use the custom host you just defined. This ensures the correct SSH key is used.

  • To clone a personal repo:

      git clone git@github.com-personal:your-personal-username/personal-repo.git
    
  • To clone a work repo:

      git clone git@github.com-work:your-work-username/work-repo.git
    
  • To clone a client1 repo:

      git clone git@github.com-work:your-client1-username/client1-repo.gitYour
    

system is now fully configured to manage both accounts automatically.

5. Update Your Git Repository's Remote URL

Finally, navigate to the local repository you want to fix and tell it to use the new SSH URL.

  • For a personal repository (like your personal project):

      cd ~/Personal/vazid
      git remote set-url origin git@github.com-personal:your-personal-usernmae/repo.git
    
  • For a work repository:

      cd ~/Work/my-work-project
      git remote set-url origin git@github.com-work:your-work-username/work-repo.git
    
  • For a work repository:

      cd ~/client1/my-work-project
      git remote set-url origin git@github.com-client1:your-client1-username/client1-repo.git
    

You are now fully set up to push and pull using the correct account automatically.

Thank You

0
Subscribe to my newsletter

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

Written by

Sheikh Vazid
Sheikh Vazid