Managing Multiple GitHub Accounts on macOS: Configuring Git for Seamless Switching

To configure multiple GitHub accounts on macOS Terminal and ensure you’re using the correct account for each repository, you can set up SSH keys and configure your Git repositories. Here’s how to do it:

1. Create SSH keys for each GitHub account

First, create separate SSH keys for each of your GitHub accounts:

# Create SSH key for your first account
ssh-keygen -t ed25519 -C "email1@example.com" -f ~/.ssh/github-account1

# Create SSH key for your second account
ssh-keygen -t ed25519 -C "email2@example.com" -f ~/.ssh/github-account2

# Example
ssh-keygen -t ed25519 -C "8797745+CoderTag@users.noreply.github.com" -f ~/.ssh/github-account-codertag
ssh-keygen -t ed25519 -C "83952949+kauzik911@users.noreply.github.com" -f ~/.ssh/github-account-kauzik

ls
github-account-codertag     github-account-codertag.pub github-account-kauzik       github-account-kauzik.pub 

2. Add the SSH keys to your GitHub accounts

Add each public key to its corresponding GitHub account:

  1. Copy the first key:
cat ~/.ssh/github-account1.pub | pbcopy

# Example
cat github-account-codertag.pub| pbcopy
  1. Go to GitHub → Settings → SSH and GPG keys → New SSH key, paste the key and save
  2. Repeat for the second account using github-account2.pub

3. Create an SSH config file

Create or edit your SSH config file:

>nano ~/.ssh/config

# First GitHub account
Host github.com-account1
   HostName github.com
   User git
   IdentityFile ~/.ssh/github-account1
   IdentitiesOnly yes

# Second GitHub account
Host github.com-account2
   HostName github.com
   User git
   IdentityFile ~/.ssh/github-account2
   IdentitiesOnly yes


# Example
> cat config

# First GitHub account
Host github.com-account-kauzik
   HostName github.com
   User git
   IdentityFile ~/.ssh/github-account-kauzik
   IdentitiesOnly yes

# Second GitHub account
Host github.com-account-codertag
   HostName github.com
   User git
   IdentityFile ~/.ssh/github-account-codertag
   IdentitiesOnly yes

Configure repositories to use the correct account

For existing repositories:

# Navigate to repository for account1
cd /path/to/repo1
git config user.name "Your Name for Account1"
git config user.email "email1@example.com"
git remote set-url origin git@github.com-account1:username1/repo-name.git

# Navigate to repository for account2
cd /path/to/repo2
git config user.name "Your Name for Account2"
git config user.email "email2@example.com"
git remote set-url origin git@github.com-account2:username2/repo-name.git

# If repository is under an organisation then username will be replace by organisation name
git@github.com-account-kauzik:ORGANIZATION/REPOSITORY.git
git remote set-url origin origin git@github.com-account1:ORGANIZATION/repo-name.git

# For new repositories, use the appropriate remote URL when cloning:
# Clone using account1
git clone git@github.com-account1:username1/repo-name.git

# Clone using account2
git clone git@github.com-account2:username2/repo-name.git


########################################
# Example
#########################################
# navitate to the repo
cd Abc/abcforall-testsuits
git config user.name "kauzik911"
git config user.email "83952949+kauzik911@users.noreply.github.com"
git remote set-url origin git@github.com-account-kauzik:Abc/abcforall-testsuite.git 

5. Test your configuration

Test each SSH connection.You should see a message like “Hi username! You’ve successfully authenticated…”

This setup ensures that when you work with repositories from different accounts, Git will use the appropriate SSH key and user identity based on how you’ve configured each repository’s remote URL.

ssh -T git@github.com-account1
ssh -T git@github.com-account2

# Example
> ssh -T git@github.com-account-kauzik
The authenticity of host 'github.com (20.207.73.82)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
Hi kauzik911! You've successfully authenticated, but GitHub does not provide shell access.

>git remote -v
origin git@github.com-account-kauzik:ABC/abcforall-testsuite.git (fetch)
origin git@github.com-account-kauzik:ABC/abcforall-testsuite.git (push)

6. Check withGit commands

git pull origin main  
ERROR: The 'abc' organization has enabled or enforced SAML SSO.
To access this repository, you must use the HTTPS remote with a personal access token or SSH with an SSH key and passphrase that has been authorized for this organization.
Visit https://docs.github.com/articles/authenticating-to-a-github-organization-with-saml-single-sign-on/ for more information.

fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

This error message provides important information about the repository access restrictions. The Pfizer organization has enabled SAML SSO (Single Sign-On), which requires special authentication methods. Let’s fix this:

Using SSH with SAML SSO

When an organization has SAML SSO enabled, you need to authorize your SSH key specifically for that organization. Here’s how to fix it:

  1. First, make sure you’re logged into your GitHub account in a web browser.
  2. Go to GitHub → Settings → SSH and GPG keys and confirm your SSH key is added to your GitHub account.
  3. You need to authorize your SSH key specifically for the ABC organization:
  4. Alternatively, you can use HTTPS with a personal access token (PAT) that has SSO enabled:
    • Create a PAT at GitHub → Settings → Developer settings → Personal access tokens
    • When creating the token, enable SSO for the Pfizer organization
    • Then update your repository to use HTTPS with your token:
# (Replace USERNAME with your GitHub username and TOKEN with your personal access token)
git remote set-url origin https://USERNAME:TOKEN@github.com/pfizer/pfizerforall-testsuite.git

> git pull origin main 
From github.com-account-kauzik:ABC/abcforall-testsuite
 * branch            main       -> FETCH_HEAD
Already up to date.

Cloning a New repo from the private organisation

Instead of 

git clone git@github.com:<ORG_NAME>/agentic-automation.git

Use the following

git clone git@github.com-account-kauzik:<ORG_NAME>/agentic-automation.git

>git clone git@github.com-account-kauzik:<ORG_NAME>/agentic-automation.git
Cloning into 'agentic-automation'...
remote: Enumerating objects: 382, done.
remote: Counting objects: 100% (382/382), done.
remote: Compressing objects: 100% (295/295), done.
remote: Total 382 (delta 67), reused 350 (delta 52), pack-reused 0 (from 0)
Receiving objects: 100% (382/382), 18.20 MiB | 1.90 MiB/s, done.
Resolving deltas: 100% (67/67), done.
Search

Table of Contents

You may also like to read

AI Driven Test & Automation

Selenium