setup multiple git accounts on one local machine (2nd iteration)
Published: 2022-12-01 18:08:51

This post is in continuation of the [original post](https://dev.to/kakarr0tt0/setup-multiple-git-accounts-on-one-local-machine-3akj) that will help you understand expected outcome if you decide to add additional git account to your local machine.

**_I highly suggest you to head over to original post if you are doing this for first time. This post is only helpful for second or successive addition of git account to same local machine._**

---
## <u>1. generate SSH key</u>

```console
$ ssh-keygen -t ed25519 -C "your_professional_email@example.com" -f ~/.ssh/<2nd_ssh_key_name>
```
I named my second key as `GitHub_key`.

## 2. <u>add passphrase to SSH key</u>
Running the above command will prompt for adding a passphrase.
```console
$ ls -la ~/.ssh/
total 112
<snip>
-rw-------   1 user  staff   484 Feb 29 13:26 GitHub_key
-rw-r--r--   1 user  staff   113 Feb 29 13:26 GitHub_key.pub
<snip>
```
Public - Private key pair will be generated in the `.ssh` folder.

## 3. <u>add SSH key to `ssh-agent`</u>

```console
$ eval "$(ssh-agent -s)" && \
ssh-add -K ~/.ssh/<2nd_ssh_key_name>
```

## 4. <u>setup SSH `config` file</u>
From the previous post, file named as `config` is present in the `.ssh` directory.

There are multiple ways to add the lines of text in this file, however I will provide with two of the easiest ways:

###4.1. add text from text editor
I use `sublime text` as my go to text editor.
```console
$ subl ~/.ssh/config
```
* `subl` followed by the path name `~/.ssh/config` will open the `config` file in the sublime text editor.
```
Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/<2nd_ssh_key_name>
```
Add the above lines of text as it is to the file and save it.

###4.2. add text from the terminal
In case you don't know how to open `config` file in text editor, you can get this on the terminal itself.
```console
$ echo "Host *" > ~/.ssh/config
$ echo "  AddKeysToAgent yes" >> ~/.ssh/config
$ echo "  UseKeychain yes" >> ~/.ssh/config
$ echo "  IdentityFile ~/.ssh/<2nd_ssh_key_name>" >> ~/.ssh/config
```

Your config file should look like the below snippet
```console
Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/<ssh_key_name>

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/<2nd_ssh_key_name>
```
## 5. <u>copy SSH public key</u>
You need to copy the contents of the generated public key to add the SSH key to the Git Service Provider account.
```console
$ pbcopy < ~/.ssh/<ssh_key_name>
```

## 6. <u>create workspace</u>
Below is the Folder Structure I am using as my workspace to neatly divide the multiple git service provider repos.
```
/~/
    |__.gitconfig
    |__/git_repos/
        |__GitHub/
            |__.gitconfig.GitHub
        |__GitLab/
            |__.gitconfig.GitLab
```
You can structure your workspace in any way but remember the path for the additional `.gitconfig.<name>`. These git configs are going to override the global git config.

## 7. <u>setup git config</u>
```console
$ git config -f .gitconfig.<name> --add user.email <your_github_email@example.com>
$ git config -f .gitconfig.<name> --add user.name <your_user_name>
$ git config -f .gitconfig.<name> --add github.user <your_github_user_name>
$ git config -f .gitconfig.<name> --add core.sshCommand "ssh -i ~/.ssh/<2nd_ssh_key_name>"
```
After running all the `git config` commands your `.gitconfig.<name>` file should look like the below snippet.
```
# ~/git_repos/GitHub/.gitconfig.GitHub

[user]
	email = <your_github_email@example.com>
	name = <your_user_name>
[github]
	user = <your_github_name>
[core]
	sshCommand = ssh -i ~/.ssh/<2nd_ssh_key_name>
```

Now you need to specify the additional `.gitconfig.<name>` file in the Global `.gitconfig`.
```console
$ git config -f .gitconfig.<name> --add includeIf."gitdir:~/git_repos/GitHub/".path ~/git_repos/GitHub/.gitconfig.GitHub
```
You Global `.gitconfig` should now look like the below snippet.
```
<snip>
[includeIf "gitdir:~/git_repos/GitLab/"]
	path = /Users/mac_user/git_repos/GitLab/.gitconfig.GitLab
[includeIf "gitdir:~/git_repos/GitHub/"]
	path = /Users/mac_user/git_repos/GitHub/.gitconfig.GitHub
<snip>
```
I have snipped the output of what Global `.gitconfig` could look like to avoid confusion as your `.gitconfig` may have some more config (which is fine).