Skip to content

Instantly share code, notes, and snippets.

@Jonalogy
Last active April 22, 2024 08:04
Show Gist options
  • Save Jonalogy/54091c98946cfe4f8cdab2bea79430f9 to your computer and use it in GitHub Desktop.
Save Jonalogy/54091c98946cfe4f8cdab2bea79430f9 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. The config file is where you draw relations of your SSH keys to each GitHub (or Bitbucket) account, and all your SSH keys generated are saved into .ssh directory by default. You can navigate to it by running cd ~/.ssh within your terminal, open the config file with any editor, and 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.

  • GitHub has this step in detail. We're not adding the keys to the ssh-agent.

2. Register your keys to the respective GitHub accounts.

  • Follow these steps 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 your respective repository

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 -e and add:
[user]
    name = user1
    email = user1@gmail.com

6. Ensure your remote url is in the right format e.g: git@github.com-user1:user1/your-repo-name.git your-repo-name_user1

  • You either run git remote set-url origin git@github.com-user1:user1/your-repo-name.git your-repo-name_user1
  • Or amend your remote ssh-url in your local git config file:
 [remote "origin"] 
       url = git@github.com-user1:user1/your-repo-name.git
       fetch = +refs/heads/*:refs/remotes/origin/*

Now you can git actions (pull/push/fetch...etc) all you like!

Resources:

Special thanks to @pbuditi for your help!

@kwam1na
Copy link

kwam1na commented Jun 14, 2023

the hero we needed but don't deserve

@DLoBoston
Copy link

This is helpful again, years later. Also, LOVE @kwam1na comment. Exactly!

@tamili2i
Copy link

What if the repo is in the organization how to set the remote URL for it.
git@github.com-my-secondary-account:organizationname/my-repo.git
is this correct?

@tsoli0321
Copy link

@tamili2i - Your line worked for me. I keep forgetting how to do this every time I have a new org repo to clone locally.
Thanks!

@Manishiiitbhopal23
Copy link

currently, I am working with GitHub let's assume with the email: xyz@gmail.com and I have another GitHub with an email linked with abc@gmail.com How I can work on both GitHub accounts with the same terminal Please suggest!

@tamili2i
Copy link

tamili2i commented Dec 8, 2023

@Manishiiitbhopal23

Configure ssh for two accounts

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

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

Clone Repositorys like below

Repository1 - git clone git@github.com-xyz:user1/your-repo-name.git your-repo-name_user1
Repository2 - git clone git@github.com-abc:user2/your-repo-name.git your-repo-name_user2

@hile
Copy link

hile commented Dec 11, 2023

There is another way to do this with SSH: create SSH configuration aliases for the hosts to define separate keys and change the origin URLs in SSH string to use these alias hosts.

In .ssh/config

Host personal.github.com
  User personalid
   IdentityFile~/ .ssh/personal-ssh-key
   HostName github.com
Host work.github.com
  User workaccount
  IdentityFile ~/.ssh/work-ssh-key
  HostName github.com

And then change origin URL in repo config to use git@personal.github.com:whatever or git@work.github.com:somethingelse kind of URLs. The git commands don't do DNS lookups and SSH resolves the alias names for you with correct key.

@varzhonglei
Copy link

varzhonglei commented Jan 11, 2024

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