I was working with two different GitHub accounts one is my personal account and the other is my work account. I had these two accounts set up on different systems but recently my personal system had some connectivity issues so I had to use both of my GitHub accounts on my work system. I did some research on how I can achieve that. In this blog post, I will try to explain how we can solve the problem of using two GitHub accounts on the same machine.
Secure Shell Protocol(SSH)
To use two different GitHub accounts on the same machine we are going to use ssh.
💡 SSH or Secure Shell is a network communication protocol that enables two computers to communicate and share data. An inherent feature of ssh is that the communication between the two computers is encrypted meaning that it is suitable for use on insecure networks. You can read more about ssh here
Let’s Get Into It.
-
Create SSH Key
We will need to create an ssh key for all the accounts that we are going to use on the machine.
To do that open the terminal and move into the ssh directory
💡 `$ cd ~/.ssh`Now we need to generate an ssh key. The syntax for the generation of an ssh key is
💡 `ssh-keygen -t rsa -C "email-address" -f "github-username"`- -C option stands for comment which helps identify the ssh key
- -f option stands for file name which means that whatever name you pass after -f that name will be the name of our ssh key file inside the ssh directory.
After understanding how we can create the ssh key let’s now create the ssh key for our two accounts
Personal Account
💡
ssh-keygen -t rsa -C "mypersonalemail@gmail.com" -f "personal"
Work Account
💡
ssh-keygen -t rsa -C "myworkemail@gmail.com" -f "work"
After entering the command the terminal will ask for a passphrase, leave it empty and proceed.
What this command will do is it will create two files one with
.pub
extension and one without any extension. In our case four files will be created in the ssh directory namedpersonal
, `personal.pub,
workand
work.pub` Add SSH keys to SSH Agent
Our keys will not work until we add them in ssh-agent or they will ask for a paraphrase every time we the key. You can read more about it here.
let's add both of our key to ssh-agent
💡 ssh-add -K ~/.ssh/work
ssh-add -K ~/.ssh/personal
- Add SSH key to GitHub
Now to use these newly generated ssh keys we need to add them in our GitHub account. So the work key will go into our work GitHub account similarly our personal key will go into our Personal GitHub account.
- Now copy the key form the
.pub
extension file. - Sign in to GitHub Account
- Goto Settings > SSH and GPG keys > New SSH Key
- Paste your copied public key and give it a Title of your choice.
-
Create a Config File and Make Host Entries
After adding the key to our GitHub account we need to create a config file in the .ssh directory and make the host entry.
If the config file is not already created, create a new config file and paste the following data into it.
# githubPersonal Host personal github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/personal # githubWork Host work github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/work
Now everything is done the only thing that remains is to clone the repositories. To achieve that, copy the ssh URL of the repository that you want to clone and use the below command
💡 `git clone git@{your-username}:{owner-user-name}/{the-repo-name}.git`if I want to copy a repo from my personal account the above command will look something like this
💡 `git clone git@personal:Hat52/blood-donation.git`From now on, to ensure that our commits and pushes from each repository on the system use the correct GitHub user — we will have to configure user.email and user.name in every repository freshly cloned or existing before.
To do this use the following commands.
git config user.email "work@gmail.com" git config user.name "Hamza Ali" git config user.email "personal@gmail.com" git config user.name "Hamza Ali"
Pick the correct pair for your repository accordingly.
To push or pull to the correct account we need to add the remote origin to the project
git remote add origin git@personal:{personal-username}/{personal-repo} git remote add origin git@work:{user-name}/{work-repo}
Now you can use:
git push git pull
This approach can be used for more then two account.
That is it from this post if you have any question or suggestion please leave them in the comments.
Top comments (0)