When dealing with multiple GitHub accounts on a single machine, managing SSH keys can get a bit tricky, but it's definitely manageable with a proper setup. Since ssh-add -l is showing the wrong GitHub user (or rather, the wrong SSH key for your current context), you'll want to set up SSH to use different keys for different GitHub accounts. Here’s how you can do that:
### 1. Generate a New SSH Key (if needed)
If you haven't already, generate a new SSH key for your second GitHub account. Skip this step if you've already generated a separate SSH key for each account.
ssh-keygen -t ed25519 -C "your_email@example.com"
When prompted, give it a unique name, such as /Users/yourusername/.ssh/id_ed25519_secondgithub.
### 2. Add the SSH Keys to the ssh-agent
Ensure that both of your SSH keys are added to the ssh-agent. You can add them using the ssh-add command.
ssh-add ~/.ssh/id_ed25519_firstgithub
ssh-add ~/.ssh/id_ed25519_secondgithub
### 3. Add the SSH Keys to Your GitHub Accounts
Make sure each SSH key is added to its respective GitHub account. You do this in the GitHub UI under Settings > SSH and GPG keys.
### 4. Configure SSH to Use Different Keys for Different Domains
You’ll need to configure SSH to use the appropriate key for each GitHub account. This involves editing (or creating) the ~/.ssh/config file.
- Open your ~/.ssh/config file in a text editor:
nano ~/.ssh/config
- Add configuration blocks for each GitHub account. You'll use an alias for the second GitHub account's hostname. For example:
ssh
# Default GitHub account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_firstgithub
# Second GitHub account
Host github-second
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_secondgithub
- Save and close the file.
### 5. Use the SSH Configuration
When you clone a repository from your second GitHub account, use the alias you set up in the SSH configuration instead of the standard github.com domain. For example:
bash
git clone git@github-second:username/repository.git
For your default account, you continue using github.com normally.
### Note:
This setup directs SSH to use different keys based on the hostname (which includes aliases defined in your SSH config). It allows seamless operation with multiple GitHub accounts by specifying which SSH key to use for authentication.
If you need to switch between accounts for existing repositories, you can update the repository's remote URL to use the appropriate hostname alias:
bash
git remote set-url origin git@github-second:username/repository.git
This approach ensures that SSH selects the correct key for each GitHub account based on the repository youre working with.