Multiple Git Profiles

Managing Multiple Gits and Github Accounts

Problem

Sometimes you just need to have more than one git accounts on the same computer. Most common use case is - one for personal use and one for work.

Solution

Have separate ssh keys for accounts. Use global config for your personal (if you are on your personal computer) and override git config per repo for work.

Generate SSH keys for each account

  • For personal account
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_personal
  • For work account
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_work

Add SSH keys to respective Github accounts

  1. Copy the contents of each public key file (e.g., ~/.ssh/id_rsa_personal.pub and ~/.ssh/id_rsa_work.pub).
  2. Go to GitHub Settings > SSH and GPG keys on each respective account.
  3. Add your SSH key as a new key for each account

Configure ssh config file

config
# Personal GitHub
Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_personal
  IdentitiesOnly yes

# Work GitHub
Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_work
  IdentitiesOnly yes

This creates aliases (github.com-personal and github.com-work) to distinguish which identity is used when connecting.

Use the correct aliases when cloning repos

  • For personal account, use:
git clone [email protected]:username/repo.git
  • For work account, use:
git clone [email protected]:company/repo.git

Configure Git user per repository

Set the Git user email and name per repository to ensure commits are associated with the correct account.

Important

This git repo must be initialized first.

cd path/to/your/repo
git config user.name "Your Name"
git config user.email "[email protected]"

Note

This should be done inside each cloned repo so your identities don’t conflict.

Add SSH keys to ssh agent

To load your identities into the agent:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_personal
ssh-add ~/.ssh/id_rsa_work