GitHub

Published: Monday, 16 May 2022

GitHub

Many open source projects host their repository on GitHub.

On 2018-10-26 it was announced that Microsoft had completed their acquisition of GitHub.

Multiple accounts

Sometimes a developer will need to push commits using a different GitHub account. This could be because the other account belongs to a different organisation. It can be achieved by using a ssh host alias.

Create a separate SSH key

A ssh key uniquely identifies a GitHub account.

First create a new ssh key for your non-default GitHub user. Use the ssh-keygen command to create a keypair, and save it an appropriate name. e.g. For an organisation called foo, run

ssh-keygen -f ~/.ssh/id_rsa_foo

Edit ~/.ssh/config

Edit your SSH config file, found in ~/.ssh/config and append an entry for the GitHub organisation.

Host foo.github.com
  HostName github.com
  IdentityFile ~/.ssh/id_rsa_foo

In the above config, foo.github.com is the SSH host pattern. If this host is used, ssh will connect to github.com using the private key found in ~/.ssh/id_rsa_foo.

Add the public ssh Key to GitHub

Login to your GitHub user that is part to the foo organisation.

Navigate to Profile -> Settings -> SSH and GPG Keys -> New SSH key

Fill in

  • Title: my_ssh_key
  • Key Type: Authentication Key
  • Key: Copy and paste the contents from ~/.ssh/id_rsa_foo.pub.

The click on Add SSH key

Cloning a repo

Copy the repo URL as you would normally do to clone it, but instead of git@github.com, use git@foo.github.com

Update the git username and email

For this repo, update the default username and email.

git config user.email foo@example.com
git config user.name "example user"