Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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!

@dongapplyboard
Copy link

dongapplyboard commented Apr 7, 2020

Thank you! I was missing Step #6 as you mentioned. I had to set which user I wanted to use in the url -> git@github.com-user1:user1/your-repo-name.git your-repo-name_user1

@david-daming
Copy link

david-daming commented May 5, 2020

Thanks! that's what I need, it's very helpful!

@bansalvks
Copy link

bansalvks commented Jun 12, 2020

Its worked like a charm and I faced only one issue and that is
when I checkout the folder structure looks like this:
/Users/vikasbansal/Documents/github/https\:/github.com/<github-username>/<project-name>.git

is it possible to change this structure? can it behave like a normal checkout where there folder structure? e.g.
/Users/vikasbansal/Documents/github/<project-name>.git

@vadorvatsal
Copy link

vadorvatsal commented Sep 14, 2020

Thanks very useful

@jiggy1com
Copy link

jiggy1com commented Nov 5, 2020

EXCELLENT!

@umairhm
Copy link

umairhm commented Jan 12, 2021

Is there a way to set the username and email in config too?

Extending the answer by @jamolpe, here is something that I have been using. Hope that helps.
1609126708255

@Lugoues
Copy link

Lugoues commented Feb 1, 2021

Thanks for this useful post! I just wanted to add that as of git 2.10 you can now specify the ssh command in the config so there is no need for ssh magic any longer. https://git-scm.com/docs/git-config#Documentation/git-config.txt-coresshCommand

So your git-personal.conf could look like this now.

[user]
  name = X X
  email = x@x.com
  signingkey = XXXXXXXXXX

[github]
  user = "x"

[core]
  sshCommand = "ssh -i  ~/.ssh/id_ed25519"

@SameerMakhija
Copy link

SameerMakhija commented Feb 2, 2021

Is there a way to set the username and email in config too?

Extending the answer by @jamolpe, here is something that I have been using. Hope that helps.
1609126708255

Worked for me. Thanks @umairhm

@abhinavdhasmana
Copy link

abhinavdhasmana commented Feb 24, 2021

Thank you for this. For me, I had to add ssh-add -K <pathtoKey> before it can be used

@DhruvSaraswat
Copy link

DhruvSaraswat commented Mar 21, 2021

Thanks for this excellent article !
After creating new ssh keys for my other account, I had to run this command -
ssh-add ~/.ssh/my_new_key
Then everything was working as expected for me.

@masihurmaruf
Copy link

masihurmaruf commented Apr 10, 2021

Sometime adding the key was not solving it for me. So i started following this sequentially. It gives more consistent result.

# list all the SSH keys attached to the ssh-agent
$ ssh-add -l
# removes all ssh entries from the ssh-agent                             
$ ssh-add -D
# Adds the relevant ssh key  
$ ssh-add ~/.ssh/relevant_ssh_key

@devaaks
Copy link

devaaks commented May 16, 2021

Sometime adding the key was not solving it for me. So i started following this sequentially. It gives more consistent result.

# list all the SSH keys attached to the ssh-agent
$ ssh-add -l
# removes all ssh entries from the ssh-agent                             
$ ssh-add -D
# Adds the relevant ssh key  
$ ssh-add ~/.ssh/relevant_ssh_key

Thanks for this. I decided to go this way as I above method didn't work for me.

@dipam7
Copy link

dipam7 commented Jun 16, 2021

Is there a way to set the username and email in config too?

Extending the answer by @jamolpe, here is something that I have been using. Hope that helps.
1609126708255

After doing this, how do you ensure you are pushing with the professional git account? The personal details are configured in the global name and email

@othonreyes
Copy link

othonreyes commented Jul 28, 2021

[core]
sshCommand = "ssh -i ~/.ssh/id_ed25519"

Thanks for this useful post! I just wanted to add that as of git 2.10 you can now specify the ssh command in the config so there is no need for ssh magic any longer. https://git-scm.com/docs/git-config#Documentation/git-config.txt-coresshCommand

So your git-personal.conf could look like this now.

[user]
  name = X X
  email = x@x.com
  signingkey = XXXXXXXXXX

[github]
  user = "x"

[core]
  sshCommand = "ssh -i  ~/.ssh/id_ed25519"

This reply was super helpful. I didn't need to edit the ~/.ssh/config file or change the remote url by using this solution. Now I can use plain git urls without any ssh magic.

@umairhm
Copy link

umairhm commented Jul 28, 2021

Is there a way to set the username and email in config too?

Extending the answer by @jamolpe, here is something that I have been using. Hope that helps.
1609126708255

After doing this, how do you ensure you are pushing with the professional git account? The personal details are configured in the global name and email

If you see the sequence, the professional conf is included after the personal one, so it changes the account automatically based on the git directory.

There is one caveat that I recently found is that we need to have a git directory to make this work. If I go to the root of the professional directory, which is not a git directory itself, I will get the personal account. A simple workaround for me was to run git init in the root of the professional directory. So now if I have to clone a new repo from my company's GitHub, I will be able to do so without any issue.

@zyadsherif
Copy link

zyadsherif commented Sep 6, 2021

Thank you

@sireesha-charyulu
Copy link

sireesha-charyulu commented Sep 16, 2021

Thank you very much

@elmarsan
Copy link

elmarsan commented Oct 15, 2021

My hero!

@loocurse
Copy link

loocurse commented Oct 20, 2021

Thank you very much :D

@rotoshine
Copy link

rotoshine commented Nov 24, 2021

Thank you!!!

@tw-yshuang
Copy link

tw-yshuang commented Dec 14, 2021

I create a CLI command to handle this!
Checkout my repo~~
This repo uses ssh-agent to switch your ssh account.

Git_SSH-Account_Switch

A CLI tool can switch an ssh account to your current shell. You will easily switch to your git account & ssh key when using the server, and using your account to manipulate the project on the server.

Installation

$ bash ./setup.sh

it will add some code in your profile & $logout_profile, and setup git-acc & .gitacc on the $HOME.
file:

git-acc.sh -> $HOME/.git-acc, git-acc function.
.gitacc -> $HOME/.gitacc, save info. that regist on git-acc.

Control

        +---------------+
        |    git-acc    |
        +---------------+

SYNOPSIS

  git-acc [account]|[option]

OPTIONS

  [account]               use which accounts on this shell, type the account name that you register.
  -h, --help              print help information.
  -add, --add_account     build git_account info. & ssh-key.
      -t, --type          ssh-key types, follow `ssh-keygen` rule, 
                          types: dsa | ecdsa | ecdsa-sk | ed25519 | ed25519-sk | rsa(default)
  -rm, --remove_account   remove git_account info. & ssh-key from this device
  -out, --logout          logout your current ssh-acc.


EXAMPLES

  $ git-acc tw-yshuang

@NateAtNTS
Copy link

NateAtNTS commented Jan 19, 2022

This is what I needed. Thanks!

@douglas-oliveira-tc
Copy link

douglas-oliveira-tc commented Feb 5, 2022

Worked like a charm, buddies!!! You guys rock!

@TraeDbot
Copy link

TraeDbot commented Mar 11, 2022

Much thanks ! 🔩⚡️

@bhatia-siddharth
Copy link

bhatia-siddharth commented Apr 21, 2022

ssh-add ~/.ssh/

worked

@giuseppe-grasso
Copy link

giuseppe-grasso commented May 10, 2022

I followed the instructions outlined above about a month ago, and everything worked correctly.

Now, after a month, I went back to make some local changes and tried to push using my-secondary-account.
However, I get the following message "Please make sure you have the correct access rights
and the repository exists."

Now, if I run:

git remote -v

I get:

origin	git@github.com-my-secondary-account:my-secondary-account/my-repo.git (fetch)
origin	git@github.com-my-secondary-account:my-secondary-account/my-repo.git (push)

which should be fine I guess.

Also, running

git config user.name
git config user.email

returns my-secondary-account and my-secondary-account-email

Lastly, opening the repo config file via

git config --local -e

I get:

[remote "origin"]
        url = git@github.com-my-secondary-account:my-secondary-account/my-repo.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
[user]
    name = my-secondary-account
    email = my-secondary-account-email

To me, these look like the exact settings that I had managed to tune, thanks to this guide, to make everything work. I fail to see where the problem is.

I would like to stress that the error message that I get now is different to what I was getting prior to setting the local config file right thanks to this guide, which was:

ERROR: Permission to my-secondary-account/my-repo.git denied to my-primary-account.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Now, I either get:

ssh: connect to host github.com port 22: Operation timed out
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

or simply:

Please make sure you have the correct access rights
and the repository exists.

If you can help me to fix this, I'd be extremely grateful. Thanks!

@iamtodor
Copy link

iamtodor commented Jul 5, 2022

For me this command git remote set-url origin git@github.com-user1:user1/your-repo-name.git your-repo-name_user1 creates a dir your-repo-name_user1 and it does not use a user1 profile

@rosatamarindus
Copy link

rosatamarindus commented Jul 6, 2022

I followed the instructions outlined above about a month ago, and everything worked correctly.

Now, after a month, I went back to make some local changes and tried to push using my-secondary-account. However, I get the following message "Please make sure you have the correct access rights and the repository exists."

Now, if I run:

git remote -v

I get:

origin	git@github.com-my-secondary-account:my-secondary-account/my-repo.git (fetch)
origin	git@github.com-my-secondary-account:my-secondary-account/my-repo.git (push)

which should be fine I guess.

Also, running

git config user.name
git config user.email

returns my-secondary-account and my-secondary-account-email

Lastly, opening the repo config file via

git config --local -e

I get:

[remote "origin"]
        url = git@github.com-my-secondary-account:my-secondary-account/my-repo.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
[user]
    name = my-secondary-account
    email = my-secondary-account-email

To me, these look like the exact settings that I had managed to tune, thanks to this guide, to make everything work. I fail to see where the problem is.

I would like to stress that the error message that I get now is different to what I was getting prior to setting the local config file right thanks to this guide, which was:

ERROR: Permission to my-secondary-account/my-repo.git denied to my-primary-account.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Now, I either get:

ssh: connect to host github.com port 22: Operation timed out
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

or simply:

Please make sure you have the correct access rights
and the repository exists.

If you can help me to fix this, I'd be extremely grateful. Thanks!

Did you make sure that your sshCommand in your local repo is correct? If not, you could try to run git config core.sshCommand "ssh -i /path/to/key"? then you'll be asked about adding your key fingerprint (e.g 'SHA256:[key]') to the list of know hosts. Afterward, you could check git remote show origin to make sure you have the correct access rights now.

@peppegrass
Copy link

peppegrass commented Jul 6, 2022

thanks for all the suggestions, now it is working

@gamgyul
Copy link

gamgyul commented Aug 7, 2022

thanks for the post. It really helped me

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