If you are a developer, then I guess you definitely 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 into multiple accounts. How do you do that? I’ll show you how!
If you don’t already have an SSH key, you must generate a new one. If you’re unsure whether you already have an SSH key, check for the existing keys.
To check the existing key, open the terminal window and run command-
cd ~/.ssh && ls
If you don’t see any key here, create a new one. We’ll create keys for every account.
If you don’t want to re-enter your passphrase every time you use your SSH key, you can add your key to the SSH agent, which manages your SSH keys and remembers your passphrase.
2. Paste the text below, and change the email to your GitHub email address.
$ssh-keygen -t ed25519 -C "email@example.com"
Note: If you are using a legacy system that doesn’t support the
Ed25519 algorithm, use:
$ ssh-keygen -t rsa -b 4096 -C “firstname.lastname@example.org”
This creates a new ssh key, using the provided email as a label.
3. When you’re prompted to “Enter a file in which to save the key,” change its name to whatever you want (e.g., personal or company) and press Enter. This accepts the default file location. If you want you can also change the file location.
4. At the prompt, type a secure passphrase. You can leave it empty for no passphrase.
It’ll create two keys (public and private) for SSH on the location of your choosing at the time of prompt. We created keys for one account. If you want to create keys for other accounts as well, all you have to do is to repeat ‘Step 1’ and don’t forget to give it a different name.
5. Verify the created keys-
ssh-agentin the background. Run this command-
$ eval “$(ssh-agent -s)”
output: > Agent pid 59566
2. Now, add your SSH private key to the
ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace
id_ed25519 in the command with the name of your private key file.
$ ssh-add ~/.ssh/id_ed25519
To configure your GitHub account to use your new (or existing) SSH key, you’ll also need to add it to your GitHub account-
2. Open the GITHUB website and log in with your account.
3. In the upper-right corner of any page, click on your profile picture, then click Settings.
4. In the user settings sidebar, click on ‘SSH and GPG Keys’.
5. Click on ‘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”. Also, paste your public key in the key section.
7. Click Add SSH key.
We’ve done the bulk of the workload but now we need a way to specify when to push to our personal account and when 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
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 if 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
git@github-COMPANY is our custom host from the config file which we created earlier,
Company is our GitHub username where repo is hosted whereas,
testing.git is our repo name.
Note that, this time, rather than pushing to
email@example.com, we’re using the custom host that we created in the config file: git@github-COMPANY.
$ cd /path/to/repo
2. Add specific(Personal or Company) key to the repo-
You will see an output as identity added. Now you are good to go. You don’t need to enter the 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 was all folks! I hope the article was helpful for you. Reach out to me in the comment section for any queries.