How to use GitHub trough multiple users on the same PC
Tags: git, github
Published: 2023-05-29 01:29:11

Referenced repo: https://github.com/tinkermakar/github-identity-101

{% youtube GlmXsTNzygU %} 

## TL;DR

Create a separate record for each account in the `~/.ssh/config` file and set user name and email for each repo individually.

## The problem

If you have to use multiple git accounts from the same PC, you may make wrong choices (like I did) and end up creating a huge mess with commits by wrong authors in wrong repos all over the place. That's due to the absence of any check on Git's side -- you can claim any identity when pushing commits.

I have been using 2 user accounts for over a year now -- one for work and one for personal projects. During this time I have tried different approaches until I landed on something straightforward, yet safe from human errors.

## How to manage git identities

Your number one enemy here is git's global config settings (`git config --global user.name ...`). If you are used to set one global git name/email combo, then it's time to unset and forget about that option. Instead, you now need to set your git identity separately in each repo you have cloned by using the same command, but without `--global`.

```bash
git config user.name John Smith
git config user.email 123123+johnsmith@users.noreply.github.com
```

If that sounds too complicated, feel free to use [this script](https://github.com/tinkermakar/github-identity-101/blob/main/github-identity/gitid.sh) to automate the process (more on that in the YouTube video above).

## How to clone repositories

Before you can even commit anything, you need to clone repos at first place. The best method here is to utilize the SSH config file (`~/.ssh/config`). If this is the first time you hear the term SSH, you'd better do some reading first, e.g. starting from [here](https://www.digitalocean.com/community/tutorials/how-to-configure-custom-connection-options-for-your-ssh-client).

In the video above I demo on the example of 2 GitHub users: `tinkermakar` and `tinkermakar-alt`. I have private SSH keys for both in my `.ssh` directory, like so:
```bash
├── tinkermakar
│   ├── id_rsa
└── tinkermakar-alt
    ├── id_rsa
```

To make use of both at the same time, I need to add a config file with the following content

```bash
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/tinkermakar/id_rsa
  UserKnownHostsFile ~/.ssh/tinkermakar/known_hosts
  PubkeyAcceptedKeyTypes +ssh-rsa

Host alt
  HostName github.com
  User git
  IdentityFile ~/.ssh/tinkermakar-alt/id_rsa
  UserKnownHostsFile ~/.ssh/tinkermakar-alt/known_hosts
  PubkeyAcceptedKeyTypes +ssh-rsa
```

Now let's break down the above:
- `HostNae`, `User` parameters are mandatory and their value is the same for all GitHub users.
- `IdentityFile` parameter is mandatory for our setup and it points at the corresponding private key.
- The `UserKnownHostsFile` is not mandatory -- it's a matter of preference on whether you want your `known_hosts` strings separate (as in the example) or all in one place.
- `PubkeyAcceptedKeyTypes` property has recently become required for some systems, so keeping it there is probably a good idea.

you can test both ssh configurations by running:
```bash
ssh git@github.com
ssh alt
```
Running the two above will result in the generation of `known_hosts` files, and the final structure of your `.ssh` directory will look similar to this:

```bash
├── config
├── tinkermakar
│   ├── id_rsa
│   ├── id_rsa.pub
│   └── known_hosts
└── tinkermakar-alt
    ├── id_rsa
    ├── id_rsa.pub
    └── known_hosts
```

Now you can git clone repos on behalf of both git users, but there is one caveat -- you may need to alter the URI of cloned repository based on the value of your SSH Host (the string right next to `Host`, which is the first line for each identity in the `~/.ssh/config` file). In the example above, hosts are equal to `github.com` and `alt`. Since Host and HostName match for the former, it can clone with no need for alterations:

```bash
git clone git@github.com:tinkermakar/github-identity-101.git
```

However, the second user cannot do the same -- git clones on the latter's behalf must replace `git@github.com` with the value of `Host`, which is `alt` in our case.

```bash
git clone alt:tinkermakar/github-identity-101.git
```

## Recap

So if you need to use multiple GitHub accounts from the same PC, then I strongly advise you to take the following steps:

1. Generate and store 2 separate pairs of RSA keys
1. Draft a `config` file to point at each separately
1. When cloning repos, alter the beginning of SSH strings with the value of `Host` for the user you want to use 
1. Set your git name and email separately for each repo (feel free to use [this script](https://github.com/tinkermakar/github-identity-101/blob/main/github-identity/gitid.sh) to streamline the process).