Peter Mekhaeil

Git: Work with multiple accounts

  • Configure multiple host aliases in ssh configuration:
# ~/.ssh/config

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa
  
Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_work
  • To use a different name and email, add conditional includes in git configuration to supply a different .gitconfig path based on working directory:
# ~/.gitconfig

[user]
    name = Default Name
    email = default.name@example.com

[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig
# ~/work/.gitconfig

[user]
    name = Work Name
    email = work.email@example.com
  • Use the new host alias as remote url:
git remote add origin git@github.com-work:username/repo.git