How to Use Your Personal Git SSH at Work
Tags: git, ssh
Published: 2022-08-25 01:53:31

---
title: How to Use Your Personal Git SSH at Work
published: true
description: Tutorial on how to set up two different SSH accounts to be used with Git.
tags: git, ssh
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
---

# Story

Since I work from home on the company laptop, sometimes I want to make a quick commit to my personal repository, without starting up my personal computer. But Github blocked password authentication in August 2021. From now on you must either use SSH keys, or configure a PAT (Personal Access Token) from Github.

I find SSH to be vastly more convenient, but my default SSH key is already taken by my workplace.

# Problem

When I push to my **personal** git account with my **work's** *(or default)* SSH key, I get this error:

```
ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
```

# Wrong solutions
1. Use the same SSH key for both personal and work computers. **Do not do that.**

That is a security issue you do not want to deal with. Do not copy an SSH key from your home machine to your work laptop, and vice versa.

2. Adding laptop's SSH key to your personal GitHub account.

Not recommended, because your work's SSH `.pub` file contains your work email, and you don't want that associated with your personal GitHub account.

# Solutions

## 1. Configuring SSH and Git

Create separate configuration files for SSH, and make Git use those separate SSH files.

You must create two separate `.ssh/config` files for each GitHub account you want to use. Each config file must specify a separate identity that you want SSH to use, for each account. You must [generate separate identity keys](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent) for your accounts beforehand.

```sh
host github.com
user git
identityfile ~/.ssh/id_ed25519 # indentity key, without .pub ending
```

This way, I created two separate config files: `~/.ssh/work_config` and `~/.ssh/personal_config`

Each config has a different `identityfile` being used.

### the Git portion

On your work laptop, you might prefer to have global git config to be your job's git config. And then configure your personal repositories locally (isolated to a single repository) with `git config --local`. That's the strategy that I use.

For your work/global git configuration, you can simply type:

```bash
git config --global core.sshCommand "ssh -F ~/.ssh/work_config"
```

after that, you can go to your personal repository and configure `git` the way you'd configure it on your personal machine.

```bash
git config --local user.name # your personal config
git config --local user.email # that you use on your personal machine
git config --local core.sshCommand "ssh -F ~/.ssh/personal_config"
```

This way, git will use different `ssh` commands, with different identity files for your personal and work-related projects.

## 2. Solution without the `core.sshCommand` change
As suggested by a comment, here is an alternative solution that I've read through. Very clever approach as well: 
https://dev.to/jimzandueta/setup-your-ssh-configuration-file-1m9e

You're welcome to ask questions in the comments, and I'll make appropriate edits.