Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save trangtungn/7b5609b74b7bf014c99f717382e0f9d7 to your computer and use it in GitHub Desktop.
Save trangtungn/7b5609b74b7bf014c99f717382e0f9d7 to your computer and use it in GitHub Desktop.
How To Work With Multiple Github Accounts on your PC

How to Work with Multiple GitHub Accounts on a Single Machine

Let’s assume you have two GitHub accounts:

  1. https://github.com/work-account
  2. https://github.com/personal-account

You want to configure your Mac to manage both accounts.

Note: This guide can be extended for more than two accounts. :)

You can set it up in 5 simple steps:

Steps:

  • Step 1: Create SSH keys for each account
  • Step 2: Add SSH keys to the SSH Agent
  • Step 3: Add the SSH public keys to GitHub
  • Step 4: Create a config file for SSH host entries
  • Step 5: Clone GitHub repositories using different accounts

Step 1: Create SSH Keys for Each Account

First, navigate to your .ssh directory:

cd ~/.ssh

Generate unique SSH keys for each account:

ssh-keygen -t rsa -C "your-email-address" -f "github-username"
  • -C adds a comment to identify your SSH key.
  • -f specifies the filename for saving the key.

For example:

ssh-keygen -t rsa -C "[email protected]" -f "github-work-account"
ssh-keygen -t rsa -C "[email protected]" -f "github-personal-account"

This creates private and public key pairs (.pub for public), named based on the value you passed with -f:

  • {github-work-account} — the private key.
  • {github-work-account}.pub — the public key.

and:

  • {github-personal-account} — the private key.
  • {github-personal-account}.pub — the public key.

Step 2: Add SSH Keys to the SSH Agent

To enable the keys for use, add them to the SSH agent:

ssh-add -K ~/.ssh/github-work-account
ssh-add -K ~/.ssh/github-personal-account

For more details on adding keys to the SSH agent, see this guide.

Step 3: Add SSH Public Keys to GitHub

Now, add the public keys to their respective GitHub accounts.

  1. Copy the public key:
pbcopy < ~/.ssh/github-work-account.pub
pbcopy < ~/.ssh/github-personal-account.pub

Add the public key to GitHub:

  • Go to Settings > SSH and GPG keys > New SSH Key.
  • Paste your key and give it a descriptive title.
  • Alternatively, visit GitHub's SSH keys settings and click New SSH Key.

Step 4: Create a Config File for SSH Host Entries

The ~/.ssh/config file lets you specify multiple SSH configurations.

If the config file doesn’t exist, create one:

touch ~/.ssh/config
open ~/.ssh/config

Add the following entries for each GitHub account:

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

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

Step 5: Clone GitHub Repositories Using Different Accounts

With the setup complete, you can now clone repositories using specific accounts.

Example:

git clone [email protected]:personal-account/TestRepo.git

Final Configuration: Set Git User Details per Repository

To ensure the correct GitHub user is used for each repository, configure the user.email and user.name settings within each repository:

git config user.email "[email protected]"
git config user.name "Your Name"

git config user.email "[email protected]"
git config user.name "Your Name"

Add the remote origin:

git remote add origin [email protected]:personal-account
git remote add origin [email protected]:work-account

Now you can push and pull with the correct account:

git push
git pull

Appendix:

Start the SSH agent

To allow git to use your SSH key, an SSH agent needs to be running on your device.

To start the agent, run:

$ eval "$(ssh-agent -s)"
> Agent pid 59566

Check the SSH agent running

To check if it is already running, run the ps command. If the ssh-agent is already running, it should appear in the output, such as:

$ ps aux | grep ssh-agent

24630 ??         0:00.00 ssh-agent -s
24660 ttys001    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox ssh-agent

// OR

$ ps -ax | grep ssh-agent

19998 ??         0:00.20 /usr/bin/ssh-agent -l

Alternatively, you can use this command to check if the SSH agent is running and properly set up:

$ echo "$SSH_AGENT_PID"

To add the SSH key to your SSH agent (ssh-agent):

Run the following command, replacing the {ssh-key-name} with the name of the private key:

ssh-add ~/{ssh-key-name}

For MacOS:

ssh-add --apple-use-keychain ~/.ssh/{ssh-key-name}

To ensure the correct SSH key is used when connecting to Bitbucket, update or create your SSH configuration file (~/.ssh/config) with the following settings:

Host bitbucket.org
  AddKeysToAgent yes
  IdentityFile ~/.ssh/{ssh-key-name}

Check if the key already added

$ ssh-add -l

256 SHA256:<fingerprint here> [email protected] (ED25519)
$ ssh -T [email protected]

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

You don't login to Git.

You do login to a Git repository hosting server, which requests an authentication, but Git itself has no authentication nor authorization.

As an example of a Git repository hosting service offering login:

  • GitHub: gh auth login
  • GitLab: glab auth login

Git itself does not log you in, as mentioned in my answer above.

A third-party CLI tool, GitHub CLI gh), can connect to a repository hosting service (github.com) and trigger the authentication.

With GitHub:

  1. create first through the Web UI a Personal access tokens (classic), with minimum required scopes: repo, read:org, and gist. Its token prefix will be ghp_....
  2. Then authenticate yourself from the command-line, after installing gh, using gh auth login.

Example:

$ gh auth login  

? What account do you want to log into? GitHub.com
? What is your preferred protocol for Git operations on this host? SSH
? Upload your SSH public key to your GitHub account? /Users/xxx/.ssh/ssh_key.pub
? Title for your SSH key: your-ssh-key
? How would you like to authenticate GitHub CLI? Login with a web browser

! First copy your one-time code: xxxx-xxxx
Press Enter to open github.com in your browser... 
✓ Authentication complete.
- gh config set -h github.com git_protocol ssh
✓ Configured git protocol
  1. Then, a gh auth status would give you:
$ gh auth status

github.com
  ✓ Logged in to github.com account gh-acc-1 (keyring)
  - Active account: true
  - Git operations protocol: ssh
  - Token: gho_************************************
  - Token scopes: 'admin:public_key', 'gist', 'read:org', 'repo'

  ✓ Logged in to github.com account gh-acc-2 (/Users/xxx/.config/gh/hosts.yml)
  - Active account: false
  - Git operations protocol: ssh
  - Token: gho_************************************
  - Token scopes: 'admin:public_key', 'gist', 'read:org', 'repo'

Then, to switch between accounts:

$ gh auth switch

✓ Switched active account for github.com to `gh-acc-2`
github-log-into-multi-accounts.mov

Reference:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment