Live with multiple Git accounts

1. Generate a new SSH key-pair for each of Git account.

  $ ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_benjaminv_github
  $ ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_bendhu_github
  $ ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_benhu1_gitlab

  # The -C option is a comment to help identify the key.
  # The -f option specifies the file name for the key pair.

2. Add the SSH keys to your SSH-agent

  $ ssh-add ~/.ssh/id_rsa_benjaminv_github
  $ ssh-add ~/.ssh/id_rsa_bendhu_github
  $ ssh-add ~/.ssh/id_rsa_benhu1_gitlab

And make these persistent for new terminal sessions by enable AddKeysToAgent yes in STEP 4.

3. Add the public keys to your GitHub accounts respectively.

Visit and use “New SSH Key”

# copy public key to clipboard without opening it
% pbcopy < ~/.ssh/
% pbcopy < ~/.ssh/
% pbcopy < ~/.ssh/

paste the public key to your GitHub account and add a name to identify it.

4. Managing Overall SSH-Keys

By default the file needed is located at ~/.ssh/config

add following lines to the end of the file

# Work github account
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519_werd_github

# Work gitlab account
  User bgit
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa_gitlab

# Personal github account
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa_benjaminv_github

5. Which Account to Use by Default?

The global Git configuration file is stored at $HOME/.gitconfig on all platforms.

This defines the default values for all Git commands.

  email = [email protected]
  name = ben
  defaultBranch = main    # stop using master as default branch

to edit it,

$ git config --global "ben"
$ git config --global "[email protected]"

6. Cloning GitHub repositories using no-default accounts

instead of using,
to match the configuration in ~/.ssh/config

6.1 Update repo already cloned before this configuration

6.1.1 Update the remote git url

% git remote set-url origin "[email protected]:benjaminv/starting-react.git"
# mind the .personal added to

6.1.2 Update the local user

% git config --local "ben"
% git config --local "[email protected]"

verify the changes by

% cat .git/config

# It looks like following

# [core]
#         repositoryformatversion = 0
#         filemode = true
#         bare = false
#         logallrefupdates = true
#         ignorecase = true
#         precomposeunicode = true
# [remote "origin"]
#         url = [email protected]:benjaminv/starting-react.git
#         fetch = +refs/heads/*:refs/remotes/origin/*
# [branch "main"]
#         remote = origin
#         merge = refs/heads/main
# [user]
#         name = ben
#         email = [email protected]

7. Confirming This is Working

% ssh -T [email protected]

# Hi benjaminv! You've successfully authenticated, but GitHub does not provide shell access.

# Mind the username here, it is benjaminv NOT my default Git account name