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!

@pbuditi

This comment has been minimized.

Copy link

@pbuditi pbuditi commented Jun 22, 2017

Thanks Jon.

@YakDriver

This comment has been minimized.

Copy link

@YakDriver YakDriver commented Aug 22, 2018

Sweet! I was able to get this to work with a default account for most of my repos, so that I don't have to enter local config for each, using the regular mac osxkeychain method and global config, and then use a local config for one-off repos that use a separate github account to use the SSH method. This was very helpful.

@peeyushsingla

This comment has been minimized.

Copy link

@peeyushsingla peeyushsingla commented Aug 26, 2018

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

@nimakarimipour

This comment has been minimized.

Copy link

@nimakarimipour nimakarimipour commented Mar 21, 2019

Thank you very much

@moonztar

This comment has been minimized.

Copy link

@moonztar moonztar commented Mar 22, 2019

Thanks

@mgonera

This comment has been minimized.

Copy link

@mgonera mgonera commented Sep 3, 2019

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

@peeyushsingla sure there is:

git config --add user.email someone@domain.com to add/override user email in given repo
git config --unset user.email someone@domain.com to remove user email entry in given repo

same for user.name of course

@mountainWanderer

This comment has been minimized.

Copy link

@mountainWanderer mountainWanderer commented Oct 25, 2019

Thanks it's very useful!

@jamolpe

This comment has been minimized.

Copy link

@jamolpe jamolpe commented Feb 29, 2020

Just a little Improvement regarding the user names in each repository:

You can configure different .gitconfig for different working directories, just setting this in your main .gitconfig (~/.gitcongit)
[includeIf "gitdir:~/[workspace]/"] path = ~/[path where is your config]/.gitconfig

and in this new .gitconfig add the [user] info you want to use in that workspace.
Also you can use visual tool like fork where you can specify to use global or different user info

@dongapplyboard

This comment has been minimized.

Copy link

@dongapplyboard 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

This comment has been minimized.

Copy link

@david-daming david-daming commented May 5, 2020

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

@bansalvks

This comment has been minimized.

Copy link

@bansalvks 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

This comment has been minimized.

Copy link

@vadorvatsal vadorvatsal commented Sep 14, 2020

Thanks very useful

@jiggy1com

This comment has been minimized.

Copy link

@jiggy1com jiggy1com commented Nov 5, 2020

EXCELLENT!

@umairhm

This comment has been minimized.

Copy link

@umairhm 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

This comment has been minimized.

Copy link

@Lugoues 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

This comment has been minimized.

Copy link

@SameerMakhija 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

This comment has been minimized.

Copy link

@abhinavdhasmana 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

This comment has been minimized.

Copy link

@DhruvSaraswat 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

This comment has been minimized.

Copy link

@masihurmaruf 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

This comment has been minimized.

Copy link

@devaaks 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

This comment has been minimized.

Copy link

@dipam7 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

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