Hi, I’m Dheeraj AKA mr-engin3er and this is my first blog on any platform ever. Before my current job, I was a freelancer and I had only one Github account where I host all my projects in private and public repos. Recently I joined a company and I struggled to manage my both GitHub accounts on a single system. So like me, if you have a personal GitHub account. Everything is working perfectly. But then, you get a new job, and now need to have the ability to push and pull to multiple accounts. How do you do that? I’ll show you how!
Step 1 — Create a New SSH Key
If you don’t have an SSH key, you must generate a new SSH key. If you’re unsure whether you already have an SSH key, check for existing keys.
To check existing key, open terminal and run command:-
cd ~/.ssh && ls
If you don't see any key create a new one. We’ll create keys for every account.
If you don’t want to re-enter your passphrase every time then you can use your SSH key, you can add your key to the SSH agent, which manages your SSH keys and remember your passphrase.
- Open Terminal in ~/.ssh directory.
2. Paste the text below, and change the email to your GitHub email address.
$ssh-keygen -t ed25519 -C “firstname.lastname@example.org”
Note: If you are using a legacy system that doesn’t support the Ed25519 algorithm, use:
$ ssh-keygen -t rsa -b 4096 -C “email@example.com”
This creates a new ssh key, using the provided email as a label.
> Generating public/private ed25519 key pair.
3. When you’re prompted to “Enter a file in which to save the key,” change its name according to your understanding(eg. personal or company) and press Enter. This accepts the default file location. If you want you can also change the file location later.
> Enter a file in which to save the key (/home/you/.ssh/id_ed25519):
4. At the prompt, type a secure passphrase. You can leave it empty for no passphrase.
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
I created the above key only for this tutorial and deleted it already.
It’ll create two keys(public and private) for ssh on the location where you run the ssh-keygen command. We created keys for one account. If you want to create keys for other accounts then repeat Step 1 and Don’t forget to give it a different name.
5. We can verify, keys are created or not.
STEP 2- Adding your SSH key to the ssh-agent.
- Start the ssh-agent in the background. Run below command
$ eval “$(ssh-agent -s)”
output: > Agent pid 59566
STEP 3- Add your SSH key to your GitHub account
To configure your GitHub account to use your new (or existing) SSH key, you’ll also need to add it to your GitHub account.
- Copy the SSH public key to your clipboard.
2. Open the GITHUB website and log in with your account.
3. In the upper-right corner of any page, click your profile photo, then click Settings.
4. In the user settings sidebar, click SSH and GPG keys.
5. Click New SSH key or Add SSH key.
6. In the “Title” field, add a descriptive label for the new key. For example, if you’re using a personal Mac, you might call this key “Personal MacBook Air”. And paste your public key in the key section.
7. Click Add SSH key.
STEP 4 — Create a Config File
We’ve done the bulk of the workload, but now we need a way to specify when we wish to push to our personal account or when we should instead push to our company account. To do so, let’s create a config file.
$ touch ~/.ssh/config
open the config file in your favorite editor.
$ vim config
If you’re not comfortable with Vim, feel free to open it within any editor of your choice. Paste in the following snippet. Don’t forget to change the file path to your file path.
#Personal GitHub account
This is the default setup for pushing to our personal GitHub account. Notice that we’re able to attach an identity file to the host. Let’s add another one for the company account. Directly below the code above in the same config file, add:
#Company GitHub account
STEP 5- Update your git config file
This time, rather than setting the host to github.com, we’ve named it github-COMPANY. The difference is that we’re now attaching the new identity file that we created previously: id_rsa_COMPANY. Save the page and exit!
If you already cloned your repository then change its remote address or first clone and then change it according to your account, if the repo belongs to the company account then enter this command or change it to personal.
$ git remote add origin git@github-COMPANY:Company/testing.git
Here git@github-COMPANY is our custom host from the config file which we created earlier, Company is our GitHub username where repo is hosted and testing.git is our repo name.
Note that, this time, rather than pushing to firstname.lastname@example.org, we’re using the custom host that we create in the config file: git@github-COMPANY.
STEP 6- Add ssh key to repo
- Go to your repo directory
$ cd /path/to/repo
2. add specific(Personal or Company )key to repo.
You will see an output as identity added. Now you are good to go. You don’t need to enter password anymore. You will need to change the repo host first time after you clone any repo and you have to add a key.
That’s all for this tutorial. If you like this blog then clap. If you wish to get amazing articles like this then follow.