How To Manage Multiple Gitub Accounts on the Same PC
Sometimes we have multiples GitHub accounts for many reasons like work, youtube, or your personal account. Probably in this moment you realize you don't know how can separate these accounts and make a push with the account you need.
The solution to this problem is using SSH keys.
1. Generating SSH keys.
In your terminal execute this command
ssh-keygen -t rsa -C “email@example.com” -f "id_rsa_work_account"
When you hit Enter you'll see this:
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Let me explain this
-t rsa means type of encryption rsa
-C this is for comment
-f is for identifiable name in this case id_rsa_work_account
You can leave the passphrase blank is up to you.
Following the previous code, we can generate a new key for a personal account like this
ssh-keygen -t rsa -C “firstname.lastname@example.org” -f "id_rsa_personal_account"
The default location of SSH keys is ~/.ssh/, now we can search our keys with the next command
ls -al ~/.ssh
You should see something like this
-rw-r--r-- 1 your_username id_rsa_personal_account
-rw-r--r-- 1 your_username id_rsa_personal_account.pub
-rw-r--r-- 1 your_username id_rsa_work_account
-rw-r--r-- 1 your_username id_rsa_work_account.pub
Alright!!! We have our keys ready.
2. Adding SSH key To GitHub Account
To connect your GitHub account with your new SSH key, we need to copy the content of the public key, don't worry the public key ends with .pub, you need to open the file and copy the content, let's move on, let's say we have to configure the work_account, login with the correspondent GitHub account and follow this steps:
Go to Settings
Select SSH and GPG keys from the menu on the left.
Click on New SSH key, provide a good name, and paste the key in the box below
- Click add SSH key button, we finish this part.
3. Register the new keys
To use the keys, we have to register them with the ssh-agent on our machine.
Execute this command in your terminal "$(ssh-agent -s)".
Add the keys to the ssh-agent like so:
One more step to finish the config.
4. Creating the SSH Config File
To make this works we need to create a config file. Follow this
- Open the location of your keys using cd ~/.ssh/
- Create a new file with touch config
- Open the config file with your editor, put this code
# Personal account
# Work account-1
We finish!!!. Let's try if this works.
5. Cloning a Repo
To clone a repo you can do this
With your personal account
git clone email@example.com_account:personal_account_name/repo_name.git
With your work account
git clone firstname.lastname@example.org_account:work_account_name/repo_name.git
6. Creating a new Repo
Create the new repository in the GitHub account and then add it as the Git remote to the local repository.
If you want to use your work account
git remote add origin email@example.com_account:work_user/repo_name.git
Thanks for reading.