Prabhakar Kasi iOS Developer, Front End Developer

Configuring multiple git accounts on new MacBook

2 min read

As a developer, after getting a new laptop there are sequence of setups and configuration one needs to do to keep the computer development ready. As part of it, configuring git is very important.

Git provides “Connect with Secure Shell Protocol (SSH)” as a convenient way to commit file to repository without the need to enter user credentials (username / password) every time. On a new computer you generally need to generate a new SSH key, setup your identity (user and password), configure default text editor and finally setup gitconfig to switch between multiple users.

  1. Generate a new SSH key and adding it to the ssh-agent following the Git Guide
# These commands are valid as of Nov 7th, 2022 - Always uses the Git Guide to get the latest commands and steps. I will try to keep this updated as accurate as possible.

# Generating a new SSH key
ssh-keygen -t ed25519 -C "pkasi@raptor.in"
 (use the default file path to save the key)
 (use a passphrase that you won't forget)

# Adding your SSH key to the ssh-agent
eval "$(ssh-agent -s)"
open ~/.ssh/config
 (replace with this config)

Host *.github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

# Add your SSH private key to the ssh-agent and store in keychain
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
  1. Configure your global identity
git config --global user.name "Prabhakar Kasi"
git config --global user.email pkasi@raptor.in
  1. Configure the default text editor
git config --global core.editor vi
  1. (Optional) Setup multiple ssh key. This is handy if you want to isolate work and personal git environment. Below I have captured steps for 1 office-git and 2 personal-github accounts.
# Generating a new SSH key - Personal Email1
ssh-keygen -t ed25519 -C "pkasi@*****.com"
 (use different file name to save the key, as we don't want to overwrite previously created file)
 (use a passphrase that you won't forget)

# Add your SSH private key to the ssh-agent and store in keychain
ssh-add --apple-use-keychain ~/.ssh/p1_ed25519

# Repeat above two commands for personal email2

# For one office-git and two personal account the ~/.ssh/config looks like this
Host git.office-pkasi
 HostName git.***.com #hostname of office-instance goes here
 AddKeysToAgent yes
 UseKeychain yes
 IdentityFile ~/.ssh/id_ed25519

Host github-pkasi
 HostName github.com
 AddKeysToAgent yes
 UseKeychain yes
 IdentityFile ~/.ssh/pkasi_ed25519

Host github-raptor
 HostName github.com
 AddKeysToAgent yes
 UseKeychain yes
 IdentityFile ~/.ssh/raptor_ed25519
  1. (Optional) Switching between different ssh key. Since office-git instance and free-personal-github.com instance has different HostName (urls) simplying adding office-ssh key would and adding free-personal instance would work by running these two commands.
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
ssh-add --apple-use-keychain ~/.ssh/pkasi_ed25519

But if we want to switch between two different free-personal-github.com account we need remove the existing private that has been added already

# To user raptor account
# Delete specific identity and then add the required identity
ssh-add -d ~/.ssh/pkasi_ed25519
ssh-add --apple-use-keychain ~/.ssh/raptor_ed25519

# To user pkasi personal account
# Delete specific identity and then add the required identity
ssh-add -d ~/.ssh/raptor_ed25519
ssh-add --apple-use-keychain ~/.ssh/pkasi_ed25519

# Use -D to delete all identities from the agent
ssh-add -D
  1. Finally we need to adding the public key to git account in order for git commands to work with proper authentication. Follow this Git guide
# Copy the public key for office-account
pbcopy < ~/.ssh/id_ed25519.pub

# Open git.office.com
Go to Settings -> SSH and GPG keys -> New SSH key

# Repeat above two steps for each accounts in github.com
  1. Verify if everything works fine by cloning some repo and push changes
git clone git@github.com:user/repo-name.git
cd repo-name
git fetch origin master
git rebase origin/master
git push -f origin master (This command will not succeed if the ssh identity is not properly configured)

Reference

  1. https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
  2. https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account
  3. https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup
Prabhakar Kasi iOS Developer, Front End Developer

Leave a Reply