Skip to content

Instantly share code, notes, and snippets.

@uraimo
Last active April 12, 2020 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uraimo/d8366c6bf8e6606a375e45b02d5281a4 to your computer and use it in GitHub Desktop.
Save uraimo/d8366c6bf8e6606a375e45b02d5281a4 to your computer and use it in GitHub Desktop.
Configuring multiple github accounts for work and private

In this short guide two sample user will be used: leisure and work.

  1. Create two ssh keys, one of each user, and register them with their github account on the site. Let's suppose you have created id_rsa_work and id_rsa_leisure.

  2. Configure ssh using two virtual hosts, one for work and one for leisure, each one linked to one of the keys:

     # Personal GitHub account
     Host leisure-github.com
       HostName github.com
       User git
       IdentityFile ~/.ssh/id_rsa_leisure
       PreferredAuthentications publickey
       PasswordAuthentication no
       IdentitiesOnly yes
    
     # Work GitHub account
     Host work-github.com
       HostName github.com
       User git
       IdentityFile ~/.ssh/id_rsa_work
       PreferredAuthentications publickey
       PasswordAuthentication no
       IdentitiesOnly yes
    

    Every time you'll want to use the leisure account clone the repo using leisure-github.com instead of github.com. Use work-github.com for the work related account.

  3. Now we need to instruct git to use a different account for different repositories. You could just set a local configuration with different username/email each time you download a new repo (perhaps through an alias) but a better idea is to add a different automatic configuration for repositories downloaded in a specific directory. In this example, let's suppose you always want to use the work account except in repositories cloned under ~/FunRepos/. Structure your global ~/.gitconfig this way and create a new ~/.gitconfig-leisure:

    [user]
      name = Work User
      email = user@work.com
      signingkey = 123456789
    [github]
      user = work
      token = 123456787654323456765432
    [commit]
      gpgsign = true
    
    ....
    .... end of .gitconfig ....
    
    [includeIf "gitdir:~/FunRepos/"]
    path = .gitconfig-leisure
    

Content of .gitconfig-leisure:

     [user]
        name = Leisure User
        email = "user@fun.com"
        signingkey = 
      [github]
        user = Leisure
        token = 
      [commit]
        gpgsign = false

All repositories cloned under ~/FunRepos will use the second configuration. It's probably necessary to override the main configuration here if you want to reset some parameters (int his case done for signingkey, token, etc...)

That's it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment