Multiple git accounts

December 08, 2015

Let's suppose you have a personal git account and you don't want use it for professional purpose, how could you work with both handily?

So, you should have two ssh keys in your folder ~/.ssh. Assume id_rsa is your personal key and id_rsa_pro your professional key. We will change the ssh configuration in order to use your professional account.

~/.ssh/config:

# My professional account
Host github-pro
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_pro

Hence, when cloning a repository, instead of using
git clone git@github.com:MyCompany/Project.git, you will clone this way:

git clone github-pro:MyCompany/Repository.git

If you have already cloned your project, you can change the remote:

git remote set-url origin github-pro:MyCompany/Repository.git

Finally, for each pro repository, you'll have to change locally your email and username:

cd /pro/repository
git config user.name "ProName"
git config user.email "ProEmail"

References:

  1. Quick Tip: How to Work with GitHub and Multiple Accounts - code.tutsplus.com
  2. Simplify Your Life With an SSH Config File - nerderati.com
  3. SSH/OpenSSH/Keys - ubuntu.com
  4. Generating SSH keys - github.com
  5. Changing a remote's URL - github.com