Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using multiple github accounts with ssh keys

Problem

I have two Github accounts: oanhnn (personal) and superman (for work). I want to use both accounts on same computer (without typing password everytime, when doing git push or pull).

Solution

Use ssh keys and define host aliases in ssh config file (each alias for an account).

How to?

  1. Generate ssh key pairs for accounts and add them to GitHub accounts.

  2. Edit/Create ssh config file (~/.ssh/config):

    # Default github account: oanhnn
    Host github.com
       HostName github.com
       IdentityFile ~/.ssh/oanhnn_private_key
       IdentitiesOnly yes
       
    # Other github account: superman
    Host github-superman
       HostName github.com
       IdentityFile ~/.ssh/superman_private_key
       IdentitiesOnly yes
    
  3. Add ssh private keys to your agent:

    $ ssh-add ~/.ssh/oanhnn_private_key
    $ ssh-add ~/.ssh/superman_private_key
  4. Test your connection

    $ ssh -T git@github.com
    $ ssh -T git@github-superman

    With each command, you may see this kind of warning, type yes:

    The authenticity of host 'github.com (192.30.252.1)' can't be established.
    RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:
    Are you sure you want to continue connecting (yes/no)?

    If everything is OK, you will see these messages:

    Hi oanhnn! You've successfully authenticated, but GitHub does not provide shell access.
    Hi superman! You've successfully authenticated, but GitHub does not provide shell access.
  5. Now all are set, just clone your repositories

    $ git clone git@github-superman:org2/project2.git /path/to/project2
    $ cd /path/to/project2
    $ git config user.email "superman@org2.com"
    $ git config user.name  "Super Man"

Done! Goodluck!

@devcdcc

This comment has been minimized.

Copy link

@devcdcc devcdcc commented Apr 22, 2019

Thanks :D

@bygui86

This comment has been minimized.

Copy link

@bygui86 bygui86 commented Dec 11, 2019

great!! thanks a lot!! 👍

@sergiotapia-papa

This comment has been minimized.

Copy link

@sergiotapia-papa sergiotapia-papa commented Dec 27, 2019

Thank you my dude!

@dorancemc-globant

This comment has been minimized.

Copy link

@dorancemc-globant dorancemc-globant commented Feb 7, 2020

Thanks!

@Semenyuk-E

This comment has been minimized.

Copy link

@Semenyuk-E Semenyuk-E commented Apr 3, 2020

Thank you so much!

@marcusinet

This comment has been minimized.

Copy link

@marcusinet marcusinet commented Apr 17, 2020

Thanks!

@tamzidoronno

This comment has been minimized.

Copy link

@tamzidoronno tamzidoronno commented May 20, 2020

Super helpful! Thanks man!

@emusvibe

This comment has been minimized.

Copy link

@emusvibe emusvibe commented Jun 3, 2020

What you need to do is to create a new ssh key for your second Github account because by default git is using the key from your first Github account and when you try to push from the second account you will get remote: Permission to privateuser/privaterepo.git denied to workuser. workeruser in this case is your first Github account's username. So you cant push to your second Github account using the first Github account's username. Follow this link for help.

https://www.youtube.com/watch?v=fnSRBRiQIU8

@MarcoF09

This comment has been minimized.

Copy link

@MarcoF09 MarcoF09 commented Jul 2, 2020

Thanks, I have a problem that maybe someone know what is it coming from. When I restart the computer the ssh-agents get deconfigured and I need to set up again.
Has anyone come across this problem?

@CypherPoet

This comment has been minimized.

Copy link

@CypherPoet CypherPoet commented Jul 5, 2020

This is great, thanks!

One potential gotcha that I encountered lies within the git clone git@github-superman:org2/project2.git /path/to/project2 step.

When cloning or adding a remote URL, I usually copy the command generated by GitHub on the repo's homepage -- which will read like git clone git@github:org2/project2.git or git remote add origin git@github:org2/project2.git.

Adding that extra -superman makes all the difference 😂

@miautoml

This comment has been minimized.

Copy link

@miautoml miautoml commented Jul 23, 2020

Adding that extra -superman makes all the difference 😂

Indeed, this is the most critical part. git@github-superman locates superman account on github. For existing repos, change remote from git@github.com:org2/project2.git to git@github-superman:org2/project2.git with,

git remote set-url origin git@github-superman:org2/project2.git
@idealltechnology

This comment has been minimized.

Copy link

@idealltechnology idealltechnology commented Jul 23, 2020

When I run this
$ ssh -T git@github-superman
I get autenticated no problem. But when I run
$ git clone git@github-superman:org2/project2.git
I get Could not resolve hostname
Using MacOs and followed the above example step by step. Anyone have an idea?

@faizanAhmed17

This comment has been minimized.

Copy link

@faizanAhmed17 faizanAhmed17 commented Aug 19, 2020

Thank you!!

@codebuilderspk

This comment has been minimized.

Copy link

@codebuilderspk codebuilderspk commented Oct 4, 2020

@gabrielfresattoJN

This comment has been minimized.

Copy link

@gabrielfresattoJN gabrielfresattoJN commented Nov 22, 2020

Nice!!

@abhishek-ejam

This comment has been minimized.

Copy link

@abhishek-ejam abhishek-ejam commented Dec 14, 2020

When I run this
$ ssh -T git@github-superman
I get autenticated no problem. But when I run
$ git clone git@github-superman:org2/project2.git
I get Could not resolve hostname
Using MacOs and followed the above example step by step. Anyone have an idea?

You should instead use:
git clone git@github.com-superman:org2/project2.git

Do no the miss the .com after github

@pwillis-els

This comment has been minimized.

Copy link

@pwillis-els pwillis-els commented Jan 1, 2021

Nice solution!

An alternative is to use Personal Access Tokens with HTTPS and prefix the user in your clone URL, such as git clone https://myuser@github.com/oanhnn/my-repository.git. This way you don't need to configure SSH on your clients or manage keys in your ssh-agent, and you get some other advantages of using HTTPS over SSH. You can keep your Personal Access Tokens in a ~/.netrc file and they will get picked up automatically by Git, or you can use your system's credentials keychain.

@jayanath

This comment has been minimized.

Copy link

@jayanath jayanath commented Jan 19, 2021

Great work! saved my day. Thank you 🙇‍♂️

@irakowski

This comment has been minimized.

Copy link

@irakowski irakowski commented Jan 22, 2021

Thank you!!! I was finally able to make it work! Lifesaver!

@youkoucoding

This comment has been minimized.

Copy link

@youkoucoding youkoucoding commented Jan 28, 2021

You save me! Thank you very much! Have a good day!

@fredrickace

This comment has been minimized.

Copy link

@fredrickace fredrickace commented Feb 20, 2021

you saved me.. Thank you...

@nielsboecker

This comment has been minimized.

Copy link

@nielsboecker nielsboecker commented Feb 22, 2021

Thanks!

@MustafaErgul

This comment has been minimized.

Copy link

@MustafaErgul MustafaErgul commented Mar 9, 2021

Is there any way to configure the local user.name and user.email within ssh config file? In other words what if I don't want to

$ git config user.email "user@mail.com"
$ git config user.name "user_name"

every time I set up my repository?

Thanks.

@mukul-kmr-jha

This comment has been minimized.

Copy link

@mukul-kmr-jha mukul-kmr-jha commented Mar 28, 2021

Thanks for the solution @oanhnn

@natanaeldgsantos

This comment has been minimized.

Copy link

@natanaeldgsantos natanaeldgsantos commented Apr 3, 2021

thank you

@SwitchTV-BenBettridge

This comment has been minimized.

Copy link

@SwitchTV-BenBettridge SwitchTV-BenBettridge commented Apr 6, 2021

Is there any way to configure the local user.name and user.email within ssh config file? In other words what if I don't want to

$ git config user.email "user@mail.com"
$ git config user.name "user_name"

every time I set up my repository?

Thanks.

Use the --global flag when setting those values - without these you're only setting the values for the current repository.

git config --global user.email "user@mail.com"

@MustafaErgul

This comment has been minimized.

Copy link

@MustafaErgul MustafaErgul commented Apr 7, 2021

Is there any way to configure the local user.name and user.email within ssh config file? In other words what if I don't want to

$ git config user.email "user@mail.com"
$ git config user.name "user_name"

every time I set up my repository?
Thanks.

Use the --global flag when setting those values - without these you're only setting the values for the current repository.

git config --global user.email "user@mail.com"

Well, this sets up global configuration. And uses the same committer information for multiple accounts.

What I am trying is that without setting up two different config per repository, I am expecting the local repository to detect which account to use when I push my code to remote. Thanks.

@benzekrimohammed

This comment has been minimized.

Copy link

@benzekrimohammed benzekrimohammed commented Apr 15, 2021

thx for the guide @oanhnn

@flowfelis

This comment has been minimized.

Copy link

@flowfelis flowfelis commented Apr 26, 2021

Just what I needed, thanks @oanhnn

Also thanks @miautoml, this was helpful

git remote set-url origin git@github-superman:org2/project2.git
@djcmm

This comment has been minimized.

Copy link

@djcmm djcmm commented May 27, 2021

Thanks alot man..

@shopkins131

This comment has been minimized.

Copy link

@shopkins131 shopkins131 commented May 28, 2021

Best documentation I have seen on this problem - many thanks guys

@chiahsien

This comment has been minimized.

Copy link

@chiahsien chiahsien commented Jun 11, 2021

It works! thanks

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