Skip to content

Connect Multiple GitHub Accounts

Effortlessly Manage Multiple GitHub Accounts in VSCode with SSH 🚀

Tired of logging in and out of GitHub just to switch accounts? It’s a hassle! 😩

💡 One-Time SSH Setup = Seamless Switching!

You might have tried solutions that require custom domain names for each account, but they come with problems:
❌ You must remember different domain names while cloning.
❌ You have to manually update git config files.

🔧 In this guide, we automate everything for you—no more manual tweaks!

🛠 Step-by-Step Guide

1️⃣ Open Git Bash & Navigate to .ssh

Terminal window
cd ~/.ssh

2️⃣ Check for Existing SSH Keys .ssh

Terminal window
ls -al ~/.ssh

If you already have an SSH key (.pub file), you can use it. Otherwise, generate a new one.

3️⃣ Generate New SSH Keys

Execute the following commands one by one:

Terminal window
ssh-keygen -t ed25519 -C "personal_email@example.com" -f ~/.ssh/<personal_github_username>
ssh-keygen -t ed25519 -C "company_email@example.com" -f ~/.ssh/<company_github_username>

Flags:

  • -C: Comment to identify the key
  • -f: Filename to save the key When prompted for a passphrase, leave it empty and press Enter.

4️⃣ Start the SSH Agent

Terminal window
eval `ssh-agent -s`

You should see an output like: Agent pid 1576

5️⃣ Add SSH Keys to SSH Agent

Terminal window
ssh-add ~/.ssh/personal_github_username
ssh-add ~/.ssh/company_github_username

6️⃣ Add Public SSH Keys to GitHub

Copy the public keys and add them to GitHub → Settings → SSH Keys or GitHub SSH Keys.

Terminal window
code ~/.ssh/personal_github_username.pub
code ~/.ssh/company_github_username.pub

7️⃣ Create & Configure SSH config File

Copy the public keys and add them to GitHub → Settings → SSH Keys or GitHub SSH Keys.

Terminal window
touch ~/.ssh/config
code ~/.ssh/config

Paste the following content:

~/.ssh/config
Host *
IgnoreUnknown AddKeysToAgent,UseKeychain
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/<personal_github_username> # Default key (You can also use company key)

This configuration allows automatic SSH key selection without needing custom domains.

8️⃣ Set Up Git Config for Different Accounts

Navigate to the home directory and create configuration files:

Terminal window
cd ~
touch .gitconfig
touch .gitconfig.company
touch .gitconfig.personal

Edit .gitconfig and add:

~/.gitconfig
[includeIf "gitdir:C:/Users/<user_name>/Personal/"]
path = ~/.gitconfig.personal
[includeIf "gitdir:C:/Users/<user_name>/Company/"]
path = ~/.gitconfig.company
[core]
excludesfile = ~/.gitignore

Edit .gitconfig.company:

~/.gitconfig.company
[user]
email = your_company_email@example.com
name = Your Name
[github]
user = "company_github_username"
[core]
sshCommand = "ssh -i ~/.ssh/company_github_username"

Edit .gitconfig.personal:

~/.gitconfig.personal
[user]
email = your_personal_email@example.com
name = Your Name
[github]
user = "personal_github_username"
[core]
sshCommand = "ssh -i ~/.ssh/personal_github_username"

This setup ensures that the correct SSH key is automatically used based on the folder location.

✅ Testing Your Setup

Now, you can clone repositories without worrying about switching accounts manually.

Terminal window
git clone git@github.com:your-personal-username/test.git # Personal
git clone git@github.com:your-company-username/test.git # Company

This Markdown file is ready to be copied and pasted into your Astro documentation! 🚀