How to Manage Multiple GitHub Accounts on One Machine (The Pain-Free Way)

If you are like most developers, you probably have two lives: your day job and your side hustles.

This usually means you have two GitHub accounts:

  1. A Work Account (e.g., specific to your organization)
  2. A Personal Account (for your open-source contributions and hobby projects)

The problem arises when you try to use both on the same laptop. You might push code to a personal repo only to realize later that the commit author is your work email. Even worse, you might encounter “Permission Denied” or “Repository Not Found” errors because Git is trying to authenticate your work repo using your personal SSH key.

Manually switching SSH keys or configuring local git settings for every single repository is tedious and prone to error.

In this post, I’ll show you a “set it and forget it” folder management strategy that automatically switches your Git identity and SSH keys based on where your project lives.

The Goal: Zero Manual Intervention

By the end of this guide, your workflow will look like this:

  • Any project inside ~/Work/ automatically uses your Work Identity and Work SSH Key.
  • Any project inside ~/Personal/ automatically uses your Personal Identity and Personal SSH Key.
  • You can clone using HTTPS or SSH, and it will just work.

Step 1: Organize Your Directory Structure

The secret sauce here is folder-based configuration. To make this work, you need to separate your projects physically on your disk.

Create two distinct directories:

mkdir ~/Personal
mkdir ~/Work

Move your existing projects into the appropriate folders.

Step 2: Generate Separate SSH Keys

If you haven’t already, generate unique SSH keys for each account.

# Generate Personal Key
ssh-keygen -t rsa -b 4096 -C "your-personal-email@example.com" -f ~/.ssh/id_rsa_personal

# Generate Work Key
ssh-keygen -t rsa -b 4096 -C "your-work-email@company.com" -f ~/.ssh/id_rsa_work

Once generated, add the public keys (.pub files) to the respective GitHub accounts under Settings > SSH and GPG keys.

Step 3: Configure SSH Config

Now, tell your computer which key to use for which “host alias.” Edit your ~/.ssh/config file:

# Personal Account
Host github.com-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_personal
    IdentitiesOnly yes

# Work Account
Host github.com-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_work
    IdentitiesOnly yes

Note: The Host value here acts as an alias we will use in the next step.

Step 4: The Magic of .gitconfig IncludeIf

This is the most critical part. Instead of putting all your settings in one global .gitconfig file, we will split them up using the [includeIf] directive.

Open your main global config (~/.gitconfig) and add this:

[user]
    # Default global user (fallback)
    email = default@example.com
    name = Default Name

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

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

This tells Git: “If I am inside the ~/Personal/ folder, load the extra settings from .gitconfig-personal.”

Step 5: Create the Specific Config Files

Now, create the two specific configuration files referenced above.

~/.gitconfig-personal

This file ensures that any repo in your personal folder uses your personal email and automatically maps URLs to the correct SSH key alias.

[user]
    name = Your Personal Name
    email = your-personal-email@example.com

# Auto-rewrite URLs to use the specific SSH alias

[url “git@github.com-personal:”]

insteadOf = git@github.com: insteadOf = https://github.com/

~/.gitconfig-work

Do the same for your work profile.

[user]
    name = Your Work Name
    email = your-work-email@company.com

# Auto-rewrite URLs to use the specific SSH alias

[url “git@github.com-work:”]

insteadOf = git@github.com: insteadOf = https://github.com/

How It Works Under the Hood

The magic lies in the [url "..."] insteadOf block.

When you run git push inside ~/Work/ProjectA:

  1. Git sees the includeIf matches ~/Work/.
  2. It loads .gitconfig-work.
  3. It sets your author email to your work address.
  4. If you cloned the repo using https://github.com/org/repo.git or git@github.com:org/repo.git, Git automatically rewrites the remote URL to git@github.com-work:org/repo.git.
  5. The SSH config sees github.com-work and uses id_rsa_work.

Summary Table

Here is a quick breakdown of how the environment handles your data:

Folder LocationGit User IdentitySSH Key Used
~/Personal/*Personal Name & Emailid_rsa_personal
~/Work/*Work Name & Emailid_rsa_work

Conclusion

By spending 10 minutes setting this up, you save yourself from:

  • Accidentally committing to work repos with a personal email.
  • Debugging SSH permission errors.
  • Having to manually configure git config user.email for every new repository you clone.

Just drag your project into the right folder, and Git handles the rest!

Leave a Comment

Your email address will not be published. Required fields are marked *