Let’s assume you have two GitHub accounts:
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:
- 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
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.
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.
Now, add the public keys to their respective GitHub accounts.
- 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.
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
With the setup complete, you can now clone repositories using specific accounts.
Example:
git clone [email protected]:personal-account/TestRepo.git
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
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
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}
$ 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:
- 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_....
- 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
- 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`