Oftentimes it may happen that you have a Github account where you upload all your code for practice sessions or learning purposes. You have another Github account pertaining to your company profile. When you push your changes, be it to your own Github account or a project-specific, how do you make sure that the respective account is always used? Well, I am here to help!
Set up SSH Keys
Let’s assume your two Github accounts are named githubPersonal and githubWork, respectively.
Create two SSH keys, saving each to a separate file:
Save it as id_rsa_personal when prompted.
Save it as id_rsa_work when prompted.
The above commands set up the following files:
- id_rsa_personal
- id_rsa_personal.pub
- id_rsa_work
- id_rsa_work.pub
Add the keys to your Github accounts
Copy the key to your clipboard:
In case you do not have pbcopy installed. To install and use pbcopy:
Edit your bash file
Create alias
Refresh your bash
Add the key to your account:
- Go to your Account Settings.
- Click “SSH Keys” then “Add SSH key”.
- Paste your key into the “Key” field and add a relevant title.
- Click “Add key” then enter your Github password to confirm.
Repeat the process for your githubWork account.
Create a configuration file to manage the separate keys
Create a config file in ~/.ssh/
Edit the file using the text editor of your choice, here I am using nano, which is readily available in Linux.
Paste the following in the config file:
Update stored identities
Clear currently stored identities:
It will show all identities removed
Add new keys:
Test to make sure new keys are stored:
Test to make sure Github recognizes the keys:
You should see something like this:
You should see something like this:
One active SSH key in the ssh-agent at a time
To keep things safe, we need to manually ensure that the ssh-agent has only the relevant key attached at the time of any Git operation. So that the active ssh key is used at the time of push
ssh-add -l will list all the SSH keys attached to the ssh-agent.
Clear currently stored identities:
ssh-add -D
Add the required ssh key
Change config name and email for a project
GitHub identifies the author of any commit from the email id attached with the commit description.
First, check the name the project is using:
Check the email the project is using:
If you need to change then do the following: