Using multiple GitHub accounts on a local machine requires managing SSH keys and configuring Git to use the correct credentials for each account. Here's how you can do it:
-
Generate a New SSH Key:
-
Open your terminal.
-
Run the following command to generate a new SSH key (replace
[email protected]
with the email associated with the GitHub account):ssh-keygen -t rsa -b 4096 -C "[email protected]"
-
When prompted, save the key with a unique name
/c/Users/USER_NAME/.ssh/id_rsa_account1
Enter file in which to save the key (/c/Users/USER_NAME/.ssh/id_rsa): /c/Users/USER_NAME/.ssh/id_rsa_account1
-
-
Ensure the ssh-agent is running by executing:
eval "$(ssh-agent -s)"
-
Add the SSH Key to the SSH Agent:
ssh-add ~/.ssh/id_rsa_account1
-
Copy the SSH Key to GitHub:
-
Copy the SSH key to your clipboard:
clip < ~/.ssh/id_rsa_account1.pub
-
Go to your GitHub account settings, then to "SSH and GPG keys," and add the key.
-
-
Repeat the above steps for the second (or additional) GitHub account. Be sure to name the keys differently (e.g.,
id_rsa_account2
).
-
Edit the SSH Config File:
- Open your
~/.ssh/config
file in a text editor. If it doesn’t exist, create it.
- Open your
-
Add Configurations for Each Account:
-
Add the following configuration, replacing the paths and usernames as needed:
# Account 1 Host github-account1 HostName github.com User git IdentityFile ~/.ssh/id_rsa_account1 # Account 2 Host github-account2 HostName github.com User git IdentityFile ~/.ssh/id_rsa_account2
-
When cloning repositories, specify the host alias you defined in your SSH config:
-
For the first account:
git clone git@github-account1:username/repo.git
-
For the second account:
git clone git@github-account2:username/repo.git
Inside each repository, you can set the user name and email to match the corresponding GitHub account:
git config user.name "Your Name"
git config user.email "[email protected]"
You can also set this globally if you prefer to have a default identity:
git config --global user.name "Your Default Name"
git config --global user.email "[email protected]"
When working with existing repositories, make sure to change the remote URL to use the correct SSH alias:
git remote set-url origin git@github-account1:username/repo.git
- SSH keys: Create a unique SSH key for each GitHub account.
- SSH config: Set up your
~/.ssh/config
file to use different keys for each account. - Cloning: Use the appropriate SSH alias to clone repositories.
- Git config: Set user name and email per repository or globally as needed.
This setup allows you to manage multiple GitHub accounts on the same machine without conflicts.