How to Use Two GitHub Accounts on One Mac with Different SSH Keys

Managing two GitHub accounts on the same Mac is very common. For example, you may have:

Account 1: Personal GitHub account
Account 2: Company / DevOps / Cotocus GitHub account

The problem starts when both accounts use GitHub SSH URLs like this:

git@github.com:username/repository.git

GitHub needs to know which SSH key should be used for which account. The cleanest solution is to configure multiple SSH identities using the ~/.ssh/config file.

In this tutorial, we will configure:

github.com  → Personal GitHub account
devops      → Company / DevOps / Cotocus GitHub account

Current SSH Key Setup

In this example, the Mac already has two SSH key pairs:

cd ~/.ssh
ls -1

Output:

cotocusin
cotocusin.pub
id_ed25519
id_ed25519.pub
known_hosts
known_hosts.old

Meaning:

id_ed25519       → private key for default/personal GitHub account
id_ed25519.pub   → public key for default/personal GitHub account

cotocusin        → private key for DevOps/Cotocus GitHub account
cotocusin.pub    → public key for DevOps/Cotocus GitHub account

Your SSH directory is:

/Users/rajesh/.ssh

1. Understand the Problem

By default, GitHub SSH URLs look like this:

git@github.com:username/repo.git

When you run:

git clone git@github.com:username/repo.git

your Mac connects to:

github.com

Then SSH tries to decide which private key to use.

But if you have two GitHub accounts, SSH may choose the wrong key. This can cause errors like:

Permission denied (publickey).

or:

ERROR: Repository not found.

or GitHub may authenticate you as the wrong account.


2. Best Solution

Use SSH aliases.

We will configure:

Host github.com
    Uses ~/.ssh/id_ed25519

Host devops
    Uses ~/.ssh/cotocusin

After this setup:

git@github.com:personal-user/repo.git

will use:

~/.ssh/id_ed25519

And:

git@devops:Cotocus-QA-Teams/.github.git

will use:

~/.ssh/cotocusin

The word devops is only a local nickname on your Mac. GitHub does not see it. SSH converts it internally to github.com.


3. Add Public Keys to Correct GitHub Accounts

First, print your personal public key:

cat ~/.ssh/id_ed25519.pub

Copy the output and add it to your personal GitHub account.

GitHub path:

GitHub → Settings → SSH and GPG keys → New SSH key

Now print your DevOps/Cotocus public key:

cat ~/.ssh/cotocusin.pub

Copy the output and add it to your DevOps/Cotocus GitHub account.

Important rule:

One SSH public key can be added to only one GitHub account.

So do not add the same .pub key to both accounts.


4. Fix SSH File Permissions

SSH is strict about permissions. Run:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 600 ~/.ssh/cotocusin
chmod 644 ~/.ssh/id_ed25519.pub
chmod 644 ~/.ssh/cotocusin.pub

Also later we will set permission for SSH config:

chmod 600 ~/.ssh/config

5. Create or Edit SSH Config File

Open SSH config:

nano ~/.ssh/config

Add this configuration:

# Default GitHub account
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes
    AddKeysToAgent yes
    UseKeychain yes

# DevOps / Cotocus GitHub account
Host devops
    HostName github.com
    User git
    IdentityFile ~/.ssh/cotocusin
    IdentitiesOnly yes
    AddKeysToAgent yes
    UseKeychain yes

Save the file:

CTRL + O
Enter
CTRL + X

Now fix permission:

chmod 600 ~/.ssh/config

6. Explanation of SSH Config

Default GitHub account

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519

This means when you use:

git@github.com:username/repo.git

SSH will use this key:

~/.ssh/id_ed25519

DevOps account

Host devops
    HostName github.com
    User git
    IdentityFile ~/.ssh/cotocusin

This means when you use:

git@devops:Cotocus-QA-Teams/.github.git

SSH will actually connect to:

github.com

but it will use this private key:

~/.ssh/cotocusin

7. Add Keys to SSH Agent

Run:

ssh-add --apple-use-keychain ~/.ssh/id_ed25519
ssh-add --apple-use-keychain ~/.ssh/cotocusin

Check loaded keys:

ssh-add -l

You should see both keys listed.

On older macOS versions, this command may also work:

ssh-add -K ~/.ssh/id_ed25519
ssh-add -K ~/.ssh/cotocusin

For modern macOS, prefer:

ssh-add --apple-use-keychain

8. Test Both GitHub Accounts

Test default GitHub account:

ssh -T git@github.com

Expected output:

Hi personal-username! You've successfully authenticated, but GitHub does not provide shell access.

Now test DevOps/Cotocus account:

ssh -T git@devops

Expected output:

Hi cotocus-username! You've successfully authenticated, but GitHub does not provide shell access.

This confirms both keys are working correctly.


9. Clone Repository Using Personal Account

For your default/personal GitHub account, use normal GitHub SSH URL:

git clone git@github.com:USERNAME/REPO.git

Example:

git clone git@github.com:rajesh/myrepo.git

This will use:

~/.ssh/id_ed25519

10. Clone Repository Using DevOps/Cotocus Account

For your DevOps/Cotocus GitHub account, use devops instead of github.com.

Example:

git clone git@devops:Cotocus-QA-Teams/.github.git

This will use:

~/.ssh/cotocusin

Notice the difference:

git@github.com:Cotocus-QA-Teams/.github.git

uses the default key.

But:

git@devops:Cotocus-QA-Teams/.github.git

uses the Cotocus/DevOps key.

That small change is the whole magic.


11. Update Existing Repository Remote URL

If you already cloned a repository using the wrong URL, go inside the repository:

cd your-repo

Check current remote:

git remote -v

You may see:

origin  git@github.com:Cotocus-QA-Teams/.github.git (fetch)
origin  git@github.com:Cotocus-QA-Teams/.github.git (push)

Change it to use the DevOps alias:

git remote set-url origin git@devops:Cotocus-QA-Teams/.github.git

Check again:

git remote -v

Expected output:

origin  git@devops:Cotocus-QA-Teams/.github.git (fetch)
origin  git@devops:Cotocus-QA-Teams/.github.git (push)

Now try:

git pull
git push

12. Set Git Username and Email Per Repository

SSH decides authentication, but Git commit identity is separate.

Inside your personal repo:

git config user.name "Rajesh"
git config user.email "your-personal-email@example.com"

Inside your DevOps/Cotocus repo:

git config user.name "Rajesh"
git config user.email "common.cotocus@gmail.com"

Check current repo identity:

git config user.name
git config user.email

To check global identity:

git config --global user.name
git config --global user.email

To list all Git config values:

git config --list

13. Recommended Final Setup

Your final SSH setup should be:

~/.ssh/id_ed25519     → Default GitHub account
~/.ssh/cotocusin      → DevOps/Cotocus GitHub account

Your ~/.ssh/config should be:

# Default GitHub account
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes
    AddKeysToAgent yes
    UseKeychain yes

# DevOps / Cotocus GitHub account
Host devops
    HostName github.com
    User git
    IdentityFile ~/.ssh/cotocusin
    IdentitiesOnly yes
    AddKeysToAgent yes
    UseKeychain yes

Use this for default GitHub:

git clone git@github.com:USERNAME/REPO.git

Use this for DevOps/Cotocus GitHub:

git clone git@devops:Cotocus-QA-Teams/.github.git

14. Common Errors and Fixes

Error 1: Permission denied publickey

Error:

Permission denied (publickey).

Fix:

ssh -T git@github.com
ssh -T git@devops

Also check:

ssh-add -l

If keys are not loaded:

ssh-add --apple-use-keychain ~/.ssh/id_ed25519
ssh-add --apple-use-keychain ~/.ssh/cotocusin

Error 2: Repository not found

Error:

ERROR: Repository not found.

Possible reasons:

  1. You are using the wrong SSH key.
  2. Your GitHub account does not have access to the repository.
  3. Remote URL is using github.com instead of devops.

Check remote:

git remote -v

For Cotocus repo, it should be:

git@devops:Cotocus-QA-Teams/.github.git

not:

git@github.com:Cotocus-QA-Teams/.github.git

Fix:

git remote set-url origin git@devops:Cotocus-QA-Teams/.github.git

Error 3: Wrong GitHub username appears

Run:

ssh -T git@github.com
ssh -T git@devops

If git@devops shows the wrong username, your key may be added to the wrong GitHub account.

Check the public key:

cat ~/.ssh/cotocusin.pub

Make sure this key is added to the correct GitHub account.


Error 4: Bad owner or permissions

Error:

Bad owner or permissions on ~/.ssh/config

Fix:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/config
chmod 600 ~/.ssh/id_ed25519
chmod 600 ~/.ssh/cotocusin

15. Best Practice

Use short and meaningful aliases.

Good examples:

github.com  → default account
devops      → company account
office      → office account
client      → client account

Avoid confusing names like:

github.com-personal
github.com-cotocusin
github-account-2-long-name

They work, but they are annoying to type daily.

Your chosen setup is clean:

github.com = normal/default GitHub
devops     = company/DevOps GitHub

Final Summary

Using two GitHub accounts on one Mac is easy when you configure SSH properly.

The key idea is:

Different GitHub account = different SSH key = different SSH Host alias

Final usage:

git clone git@github.com:personal-user/repo.git

for your default GitHub account.

And:

git clone git@devops:Cotocus-QA-Teams/.github.git

for your DevOps/Cotocus account.

This setup keeps both accounts clean, avoids authentication conflicts, and makes daily Git work much smoother.

Related Posts

Low-Level Authentication Flow Design for Student, Trainer, and Consultant Services Using Keycloak

Yes — the clean low-level flow should use Keycloak as the central Identity Provider, and Student / Trainer / Consultant services should never handle passwords directly. Recommended…

Read More

Github: GitHub CLI gh — Install, Authenticate, and Use GitHub from the Terminal

1. What is gh? gh is the official GitHub command-line interface. It brings GitHub features such as repositories, pull requests, issues, GitHub Actions, Codespaces, releases, secrets, variables,…

Read More

Datadog: Application Error Tracking in EKS using Datadog, DogStatsD, APM, Logs, and Error Tracking

Master Guide: Application Error Tracking in EKS using Datadog, DogStatsD, APM, Logs, and Error Tracking First, tiny naming correction: it is DogStatsD, not DogStashD. DogStatsD is Datadog’s…

Read More

MongoDB – Complete End-to-End MongoDB Tutorial Blog: From Basics to Advanced

MongoDB is a NoSQL document database. Instead of storing data in rows and columns like a relational database, MongoDB stores data as documents inside collections. These documents…

Read More