Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save biborn/3921cdc66ec8c6098bfc0ee271c3a9f0 to your computer and use it in GitHub Desktop.

Select an option

Save biborn/3921cdc66ec8c6098bfc0ee271c3a9f0 to your computer and use it in GitHub Desktop.
Handling Multiple Github Accounts on MacOS

Handling Multiple Github Accounts on MacOS

The only way I've succeeded so far is to employ SSH.

Navigate to ~/.ssh and open up the config file. By default, it should look something like this:

Host *
  AddKeysToAgent yes
  UseKeyChain yes
  IdentityFile ~/.ssh/id_rsa
  ForwardAgent yes
  1. In order for your Mac to be identified by two different GitHub accounts, you'll need to create SSH key pairs for each account.
  • run ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

  • This will prompt "Enter a file in which to save the key". The default suggested filename would be id_rsa. Remember to name the new file differently if you have any existing one, eg. user-1. This will generate a private and public key, user-1 and user-1.pub, respectively.

  1. Register the contents of the public key, e.g user-1.pub, into the respective GitHub accounts. Follow the steps here to do so

  2. Head back over to the SSH config file at ~/.ssh and amend accordingly to:

#user1 account
Host github.com-user1
   HostName github.com
   User git
   IdentityFile ~/.ssh/github-user1
   IdentitiesOnly yes

#user2 account
Host github.com-user2
   HostName github.com
   User git
   IdentityFile ~/.ssh/github-user2
   IdentitiesOnly yes

Replace user1 or user2 with your GitHub usernames

  1. Go ahead to git clone the respective GitHub account

git clone git@github.com-user1:user1/your-repo-name.git your-repo-name_user1

  1. Configure your git identity:
  • open up local git config git config --local -eand add:
[user]
   name = user1
   email = user1@gmail.com
  1. Ensure your remote url is in the format of git@github.com-user1:user1/your-repo-name.git your-repo-name_user1

Now you can git push and pull all you like!

Resources:

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