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.

Assuming you are new to this like me, first I'd like to share with you that your Mac has a SSH config file in a .ssh directory and this is where you draw relations of your SSH keys to each GitHub (or Bitbucket) account. You can navigate to it by running cd ~/.ssh within your terminal, open the config file with any editor, and by default it should look something like this:

Host *
 AddKeysToAgent yes
 UseKeyChain yes
 IdentityFile ~/.ssh/id_rsa
 ForwardAgent yes

Assuming you've got 2 github accounts, for work and play, lets get your Mac to "register" them. To do that that you'll need to create SSH key pairs for each account. If you have already setup your Mac to SSH with one of them, or check if you have one, continue on with the following for the second account.

1. Creating the SSH keys. For each SSH key pairs:

  • run ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

  • You'll be prompted: "Enter a file in which to save the key" and the suggested default filename would be id_rsa. This filename will be used for your SSH private and public keys so remember to make it unique, eg. user-1, user-2. This step will generate both the private and public keys, user-1 + user-1.pub , user-2 + user-2.pub respectively.

2. Register your keys to the respective GitHub accounts.

  • Follow the steps here to do so.

3. 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/identification-handlers

4. Go ahead to git clone the respective GitHub account

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

5. Configure your git identity:

  • Open up local git config using git config --local -eand add:
[user]
    name = user1
    email = user1@gmail.com

6. Ensure your remote url is in the right format!

  • `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