How to use multiple accounts

  1. Generate SSH Keys for both accounts

Example:

ssh-keygen -t rsa -b 4096 -f ~/.ssh/KEY1

2. Add the SSH Public Key into the respective accounts

3. Set up configuration file ~/.ssh/config with both accounts

# ACCOUNT 1, - the default config
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/KEY1

# ACCOUNT 2
Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/KEY2

4. Clone using the alias for the Github Link

git clone git@github.com:USER_NAME/GITHUB_REPO_NAME.git

or

git clone git@github.com-work:USER_NAME/GITHUB_REPO_NAME.git

Note: You will have to change only github.com to the alias you have set up

Additionally, if you need to test which account connects where use the following commands:

ssh -T git@github.com # Note this is Account 1 from ~/.ssh/config

# or

ssh -T gith@github.com-work # Note this is Account 2

Note: If you have set a different HOST parameter, you will have to use that one

As long as you have set up the SSH Keys correctly and the configuration file you should be able to clone the repositories successfully and the output should look like this:

Cloning into 'REPO_NAME'...
remote: Enumerating objects: 1138, done.
remote: Counting objects: 100% (1138/1138), done.
remote: Compressing objects: 100% (886/886), done.
remote: Total 1138 (delta 198), reused 1124 (delta 187), pack-reused 0
Receiving objects: 100% (1138/1138), 9.09 MiB | 1.26 MiB/s, done.
Resolving deltas: 100% (198/198), done.

Note: If you are still unable to clone the Repo this might be due to an account conflict due to an old account.

You might need to clear all accounts and not configure any of them

git config --global --unset user.name
git config --global --unset user.email

# To check use:
git config --list

Check again account connectivity with:

ssh -T git@github.com
ssh -T git@github.com-work

5. After you have pulled the repository, you will need to add the following field to the ~/REPO_NAME/.git/config file

[user]
        name = USER_NAME
        email = EMAIL@email.com

Should look something like this:

[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[remote "origin"]
        url = git@ALIAS-1:USER_NAME/REPO_NAME.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
[user]
        name = USER_NAME
        email = EMAIL@email.com

Last updated