How to Use Multiple GitHub Accounts
If you have multiple GitHub accounts, it can be a challenge to manage them on the same computer. Fortunately, with a few configuration changes, you can easily use multiple GitHub accounts.
Here are the steps to set up multiple GitHub accounts:
1. Generate SSH keys for each account
First, you need to generate SSH keys for each GitHub account you want to use. You can use the following command to generate an SSH key for your "Account 1" GitHub account:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
Be sure to replace "[email protected]" with the email address associated with your "Account 1" GitHub account. When prompted, save the SSH key to the default location (~/.ssh/id_rsa).
Repeat this process for each GitHub account you want to use, using a unique name for each SSH key (e.g., id_rsa_account1 for "Account 1", id_rsa_account2 for "Account 2", etc.).
2. Add SSH keys to GitHub
Next, you need to add the SSH keys to each GitHub account. Log in to your "Account 1" GitHub account and go to the "Settings" page. Click on the "SSH and GPG keys" tab, then click the "New SSH key" button. Paste the contents of the id_rsa_account1.pub file (located in ~/.ssh/) into the "Key" field and give the key a descriptive name. Click "Add SSH key" to save the key.
Repeat this process for each GitHub account you want to use, using the appropriate SSH key for each account.
3. Create a configuration file
Next, you need to create a configuration file for SSH to specify the different hosts and SSH keys to use for each GitHub account. Use the following command to create a new configuration file (or open the existing one if it already exists):
nano ~/.ssh/config
In the configuration file, add the following lines for each GitHub account you want to use:
# Account 1
Host github.com-account1
? HostName github.com
? User git
? IdentityFile ~/.ssh/id_rsa_account1
# Account 2
Host github.com-account2
? HostName github.com
? User git
? IdentityFile ~/.ssh/id_rsa_account2
Replace "account1" and "account2" with unique names for each GitHub account. Make sure that the IdentityFile parameter points to the correct SSH key for each account.
4. Clone repositories and set up the remote origin
Next, you need to clone the repositories you want to work with using Git. For each GitHub account, follow these steps:
git clone [email protected]:<account1-username>/<repository-name>.git
Replace <account1-username> with the username of your "Account 1" GitHub account, and <repository-name> with the name of the repository you want to clone.
领英推荐
It's important to note that I made a mistake initially by not using the Git hostname correctly for my personal account. I was using the following incorrect command:
git clone [email protected]<account1-username>/<repository-name>.git
By omitting the correct hostname, I was experiencing "access denied" errors when attempting to push or pull. Even though I had deleted all my GitHub credentials, it was still using my work Git account.
To avoid this mistake, make sure to use the correct format with the Git hostname specified as follows:
git clone [email protected]:<account1-username>/<repository-name>.git
By using the correct hostname, you ensure that Git correctly associates the repository with the respective GitHub account.
Repeat this process for each GitHub account and repository you want to work with.
5. Associate existing local code with the remote origin
If you already have existing code on your local machine that you want to associate with your GitHub accounts, you can follow these steps:
cd /path/to/repository
git remote -v
This command will display the current remote origin URL.
git remote set-url <account-remote-name> <repository-url>
Replace <account-remote-name> with the name you assigned to the remote repository for the corresponding GitHub account, and <repository-url> with the URL of the repository.
For example, if you want to update the remote origin URL for "Account 1", you can use:
git remote set-url account1 [email protected]:<account1-username>/<repository-name>.git
git remote -v
The new remote URL should be displayed.
By following these steps, you can associate your existing local code with the appropriate remote origin URL for each GitHub account. This ensures that you can push and pull changes to the correct remote repository associated with the respective account.
Software Developer | React | React Native | Node
10 个月Awesome! Helped me a lot. Thank's
Ruby On Rails | SSO | OKTA Expert | MySQL | Java
10 个月If your commits are still happening through old/work account then make sure to run below in your repository git config user.email "[email protected]"
Infrastructure Engineer | DevOps && CloudOps Engineer | SRE | Community Organizer | Infrastructure as Code | Distributed Systems
1 年In a adition when you're using multiple profiles, you should also ensure that you use the proper email and name
Commercial Data Analytics Manager at Viatel Technology Group
1 年Thank you, this resolved the issue for me ??
Backend Engineer @ GitLab
1 年Good stuff ?? I wouldn't say it MacOS specific though since you can do the same in any OS with git and ssh.