-
Open a terminal on your computer.
-
Generate a new SSH key for your first GitHub account. Replace
[email protected]
with the email associated with your GitHub account.ssh-keygen -t ed25519 -C "[email protected]"
When prompted, save the key with a descriptive name, such as
id_ed25519_first_account
. -
Repeat the process for your second GitHub account (and any additional accounts). Use a different descriptive name for each key, such as
id_ed25519_second_account
.
-
Start the SSH agent in the background:
eval "$(ssh-agent -s)"
-
Add the SSH keys to the agent. Replace
path_to_key
with the path to the SSH key files you generated earlier:ssh-add ~/.ssh/id_ed25519_first_account ssh-add ~/.ssh/id_ed25519_second_account
-
Copy the SSH key to your clipboard. Replace
path_to_key
with the path to the SSH key file:pbcopy < ~/.ssh/id_ed25519_first_account.pub
Or, if
pbcopy
is not available, you can manually copy the key:cat ~/.ssh/id_ed25519_first_account.pub
-
Add the SSH key to your GitHub account:
- Go to your GitHub account settings.
- Navigate to SSH and GPG keys.
- Click New SSH key.
- Paste the copied key and give it a descriptive title.
- Repeat for the second (and any additional) GitHub accounts.
-
Edit the SSH configuration file (
~/.ssh/config
) to define configurations for each GitHub account. If the file does not exist, create it:nano ~/.ssh/config
-
Add configurations for each account:
# Configuration for first GitHub account Host github.com-first HostName github.com User git IdentityFile ~/.ssh/id_ed25519_first_account # Configuration for second GitHub account Host github.com-second HostName github.com User git IdentityFile ~/.ssh/id_ed25519_second_account
-
Set up global Git configuration (used as a fallback):
git config --global user.name "Your Name" git config --global user.email "[email protected]"
-
Set up repository-specific Git configurations. For each repository, configure Git to use the appropriate account:
cd path/to/your/repository git config user.name "Your Name for First Account" git config user.email "[email protected]" git config core.sshCommand "ssh -i ~/.ssh/id_ed25519_first_account -F /dev/null"
For repositories associated with the second account, use the corresponding SSH key:
cd path/to/your/second-repository git config user.name "Your Name for Second Account" git config user.email "[email protected]" git config core.sshCommand "ssh -i ~/.ssh/id_ed25519_second_account -F /dev/null"
-
When cloning a repository, use the
Host
alias defined in your SSH configuration (~/.ssh/config
). For example:git clone [email protected]:username/repository.git git clone [email protected]:username/repository.git