Skip to content

Instantly share code, notes, and snippets.

@oanhnn
Last active March 27, 2024 04:48
Show Gist options
  • Save oanhnn/80a89405ab9023894df7 to your computer and use it in GitHub Desktop.
Save oanhnn/80a89405ab9023894df7 to your computer and use it in GitHub Desktop.
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
    

    NOTE: If you use any account frequently, you should use the default hostname (github.com).

  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-keyscan github.com >> ~/.ssh/known_hosts
    $ ssh -T git@github.com
    $ ssh -T git@github-superman

    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, you need remeber

    git@github-superman:org/project.git => user is superman
    git@github.com:org/project.git.     => user is oanhnn
    
  • If you need clone a repository, just do:
$ git clone git@github-superman:org1/project1.git /path/to/project1
$ cd /path/to/project1
$ git config user.email "superman@example.com"
$ git config user.name  "Super Man"
  • If you already have the repo set up, after the ssh config instructions, you need change the URL of origin, just do:
$ cd /path/to/project2
$ git remote set-url origin git@github-superman:org2/project2.git
$ git config user.email "superman@example.com"
$ git config user.name  "Super Man"
  • If you are creating a new repository on local:
$ cd /path/to/project3
$ git init
$ git remote add origin git@github-superman:org3/project3.git
$ git config user.email "superman@example.com"
$ git config user.name  "Super Man"
$ git add .
$ git commit -m "Initial commit"
$ git push -u origin master

Done! Goodluck!

Addon:

The bash script that prompts for your git account. Thank @davorpa

#!/bin/bash

# silent prompt
read -p 'GIT profile: ' profile

# switch
case $profile in
  superman)
    git config user.email "superman@example.com"
    git config user.name "superman" 
    git config user.signingKey "superman_gpg_public_key"
    ;;
  oanhnn)
    git config user.email "oanhnn@example.com"
    git config user.name "oanhnn" 
    git config user.signingKey "oanhnn_gpg_public_key"
    ;;
  # default case: raise error
  *)
    >&2 echo "ERR: Unknown profile: $profile"
    exit 1
esac
@marshallcode
Copy link

Very helpful. Thank you

@congson95dev
Copy link

Doesn't work, i got git@github.com: Permission denied (publickey).

@SofijaErkin
Copy link

SofijaErkin commented Mar 16, 2022

@saxsax1995 .
The possible reason is that the key, that is, the RSA is not paired well. You need to regenerate the key pair.
Your problem reference this Stackoverflow.
Just take a look at my RoadMap(from terminal to Git-Account via SSH).
Multiple Git-Account Using witness Step 5.

@the-code-bae
Copy link

Great guide, it helps me a lot! And I can expand this guide with another trick.

In your ~/.gitconfig

[url "git@github.com-company:company_github_account/"]
    insteadOf = git@github.com:company_github_account/

After this setting, you don't have to change URL manually every time when you want to clone repos.

God bless you for this !!! @chiahsien

And thank you @oanhnn - I've used this guide numerous times!

@chapmandu
Copy link

Great post.. Thank you!

@EdgarOrtegaRamirez
Copy link

EdgarOrtegaRamirez commented Mar 23, 2022

Make sure you don't have a global identity file set

Host *
-  IdentityFile ~/.ssh/key
+

@saingsab
Copy link

Wonderful
Saving my life....

@replyarifhussain
Copy link

Thanks for the post

@webmatrixxxl
Copy link

You are a hero!

@karolisgrinkevicius-home24

You are true superman! 🦸

@ferazog
Copy link

ferazog commented Apr 22, 2022

I've followed the above instructions, but I can't push into the remote branch.

same here, any update?

@karolisgrinkevicius-home24

@felipeerazog I would try to use the following adjustment of Hosts in ~/.ssh/config.

# Default github account: oanhnn
Host github.com-your-username-for-default-account
   HostName github.com
   IdentityFile ~/.ssh/oanhnn_private_key
   IdentitiesOnly yes
   
# Other github account: superman
Host github.com-your-username-for-superman
   HostName github.com
   IdentityFile ~/.ssh/superman_private_key
   IdentitiesOnly yes

@ferazog
Copy link

ferazog commented Apr 25, 2022

Hey @karolisgrinkevicius-home24 thanks for your comment. I found that my error was in the clone command:

# Before:
git clone git@github.com:path/to/repo
# After:
git clone git@github-superman:path/to/repo

@NelzkieCoder
Copy link

Works Thanks

@ashwinbabus
Copy link

When I try to test the connection (step 4) for the other GitHub account I got the following error,

ssh: Could not resolve hostname github-otheraccount: nodename nor servname provided, or not known

@ferazog
Copy link

ferazog commented May 9, 2022

Hey @ashwinbabus check your ~/.ssh/config file, make sure you have github-otheraccount listed as host

@dovdiienko-qlt
Copy link

dovdiienko-qlt commented May 12, 2022

Nice trick I also used for years. But as it turned out, that does not work with repos which have submodules. The .gitmodules contains URL to the linked GIT repo. In case if you use git@github-superman:... URL to the submodule repo, that might fail to work on another computer, as they do not have a superman alias

@chiahsien
Copy link

Nice trick I also used for years. But as it turned out, that does not work with repos which have submodules. The .gitmodules contains URL to the linked GIT repo. In case if you use git@github-superman:... URL to the submodule repo, that might fail to work on another computer, as they do not have a superman alias

Try to add this setting to your ~/.gitconfig and see if it works.

[url "git@github.com-company:company_github_account/"]
    insteadOf = git@github.com:company_github_account/

@dovdiienko-qlt
Copy link

dovdiienko-qlt commented May 13, 2022

Try to add this setting to your ~/.gitconfig and see if it works.

[url "git@github.com-company:company_github_account/"]
    insteadOf = git@github.com:company_github_account/

After I've added that config, I cannot clone. Git hangs.

I've found another solution which works for me:

set GIT_SSH_COMMAND=ssh -i private_key_file -o IdentitiesOnly=yes
git clone user@host:repo.git

The source: StackOverflow.com

@lucsrods
Copy link

Thanks mate!

@HuStmpHrrr
Copy link

I suggest using Match Host for a must shorter config

@JuiceBro
Copy link

JuiceBro commented Jun 7, 2022

Come from Colt Steele udemy course.
Still thanks to you!

@svascot
Copy link

svascot commented Jul 1, 2022

Thank you! this is the best post ever about this topic!

@lautarogonzalez-santex
Copy link

lautarogonzalez-santex commented Jul 5, 2022

Thanks so much. This was very helpful. A little thing you need to add in step 5 if the repo was not cloned:

git config core.sshCommand "ssh -i ~/.ssh/superman_private_key -F /dev/null"

This will let you use the superman_private_key file every time you fetch, pull, or push to the remote from project2 local repo.

Alternatively, if the repo was created locally (i.e. not cloned), setting the remote with the alias will also do that:

git remote add origin git@github-superman:org2/project2.git

Thanks!! It was very useful

@guidoprinc
Copy link

Thank for this solution and all the comments! This really helped!

@Snouzy
Copy link

Snouzy commented Jul 14, 2022

used many times on differents computers. Thanks again :)

@kgdin42
Copy link

kgdin42 commented Jul 15, 2022

Hey @oanhnn thanks for the amazing solution. I would like to add that when copying the remote repo url from github you have to edit the url to repalce github.com with github-username for the second account to work.

@anianJudy
Copy link

Hey @oanhnn thanks for the amazing solution. I would like to add that when copying the remote repo url from github you have to edit the url to repalce github.com with github-username for the second account to work.

you are right!
specialy when you clone by http url before add git config. need change remote.origin.url to git url using github-superman.
example:
git@github-superman:<username>/<projectname>.git

@morganavr
Copy link

Thanks for this guide!

@andresmascl
Copy link

This is brilliant. Thank you very much!

@ForceGT
Copy link

ForceGT commented Aug 10, 2022

https://github.com/tw-yshuang/Git_SSH-Account_Switch

Underrated stuff but all of this can be done with a command switch and no /.gitconfig stuff

@tw-yshuang
Copy link

tw-yshuang commented Aug 17, 2022

https://github.com/tw-yshuang/Git_SSH-Account_Switch

Underrated stuff but all of this can be done with a command switch and no /.gitconfig stuff

Thank you for the compliment, hope everyone can enjoy this tool, and click the star😁

@L-F-Escobar
Copy link

L-F-Escobar commented Sep 6, 2022

I had this working for a long time then suddenly today my configs stopped working.

I had to re-run ssh-add ~/.ssh/.... cmds again to properly switch between accounts. Any idea why I had to re-run ssh-add commands?

@rsbmkJ
Copy link

rsbmkJ commented Sep 13, 2022

Thanks so much, really you saved my life 👍

@khushab
Copy link

khushab commented Sep 19, 2022

Thanks 👍

@micochango
Copy link

Life saver, thank you!

@volunteerMinHtet
Copy link

Thank a lot

@dayachettri
Copy link

Thanks a lot.

@elitongadotti
Copy link

it worked here <3

@jef
Copy link

jef commented Oct 12, 2022

HUGE! Thank you :)

@MarcosiOSdev
Copy link

Thank you !
Nice.

@jdvivar
Copy link

jdvivar commented Oct 17, 2022

If you already have the repo set up, after the ssh config instructions, just do:

  • Remote is called probably origin
  • I have work and personal accounts, for this I used git@github-personal as host from your SSH config
  • user/repo-name.git for the Github repository

$ git remote set-url origin git@github-personal:user/repo-name.git

@sergiocbueno
Copy link

Thank you!

@arisanglobal
Copy link

This is really helpful tips to work with multiple GitHub repositories. Thank you!

@jkito
Copy link

jkito commented Nov 13, 2022

Thanks, really useful!

@Mtillmann
Copy link

Thanks a ton for this! I had weird issues where ssh -T git@github.com -vvv and ssh -T git@github-somethingelse -vvv both would say that they use the specified key but I got identified with the my default username on both. The solution was to create a new key and add it to the second account.

@danish9811
Copy link

will the purpose be the same for multiple bitbucket accounts, and what if we have two github and two bitbucket accounts

@zyend
Copy link

zyend commented Dec 22, 2022

Thank you!

@danish9811
Yes, I just add it to the config with the right settings.

@gurcankavakci-esen
Copy link

I had this working for a long time then suddenly today my configs stopped working.

I had to re-run ssh-add ~/.ssh/.... cmds again to properly switch between accounts. Any idea why I had to re-run ssh-add commands?

Same issue.

@abymathewtranzmeo
Copy link

abymathewtranzmeo commented Jan 13, 2023

Thanks man, I think this is the best post about this topic.

@antonga23
Copy link

You're doing the lord's work king 👑

@AlanDeikman
Copy link

Saved my life.

@kstryjewski-1
Copy link

Works, thanks :)

@JeanMeche
Copy link

Thx Bro for writing & sharing that !

@vvrmatos
Copy link

Kudos to the instructions. They work as a charm!!

@sebb-m0
Copy link

sebb-m0 commented Mar 1, 2023

👍 thx for this.

@1manfactory
Copy link

You should use this to collect the public key from GitHub.
ssh-keyscan github.com >> ~/.ssh/known_hosts
No more error: The authenticity of host 'github.com (192.30.252.1)' can't be established.

@awesomeandrey
Copy link

that's marvelous guide - it works

@chhapanbhogi
Copy link

Excellent post .

@chenxiao218
Copy link

chenxiao218 commented Mar 12, 2023

git remote set-url origin git@github-personal:user/repo-name.git

This works to me, thanks.

@gabrielbissey
Copy link

Very helpful. Thanks!

@webThreeBuilder
Copy link

If you already have the repo set up, after the ssh config instructions, just do:

  • Remote is called probably origin
  • I have work and personal accounts, for this I used git@github-personal as host from your SSH config
  • user/repo-name.git for the Github repository

$ git remote set-url origin git@github-personal:user/repo-name.git

Hi, thanks for your good tips 👍, its worked for my case by your great comment, after following the steps above, we also have to reset the remote origin url with our custom "git-acount-host" in the config

@abdeljabarTaoufikallahPro

Thanks man

@akhill4054
Copy link

Thank you man! You are a life saver!

@datatravelandexperiments

Thank you! Note that if you have ssh multiplexing set up globally, especially with persistence, you may want to disable it for github, since multiplexing works per-machine and not per Host entry. Add ControlMaster no to each Host configuration.

@silicakes
Copy link

This is golden, thanks!

@aicals
Copy link

aicals commented Jun 2, 2023

Pure gold! Thanks!

@mahe113vsp
Copy link

thank you!

@cognivator
Copy link

@jdvivar Many thanks. This host customization works great for tools like SourceTree in which you don't enter the ssh host alias directly.

@kqfu
Copy link

kqfu commented Jun 17, 2023

Great post. This is by far the cleanest solution I've found.

@aacassandra
Copy link

great! thankyou sir

@rohanrmallya
Copy link

Neat! 🔥 Thanks for this. :)

@odooerpdevelopers
Copy link

odooerpdevelopers commented Aug 16, 2023

$

Thanks bro, I had problems with this configuration since I work with docker and docker recommends these settings to use remote connections, but in github I had to uncomment those lines and it works fine :)) (~/.ssh/config)
#ControlMaster auto
#ControlPath ~/.ssh/control-%C
#ControlPersist yes

@ImadMAKS
Copy link

Thank you for this post; very clear and helpful!
I have a question regarding the host, though. When I change the origin host on my local repo, shouldn't I change it remotely in my GitHub account? I can't figure out how, though.
Maybe I am missing something, but I tried changing it only locally, and I tried to fetch I get from the same repo and I still get:

ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights, and the repository exists.

I'm wondering what I'm missing.

My config file contains the following:

Host git@github.com
        HostName github.com
	User git
	IdentityFile ~/.ssh/X.pub
	IdentitiesOnly yes
Host git@github.com-work
	HostName github.com
	User git
	IdentityFile ~/.ssh/Y.pub
	IdentitiesOnly yes

I changed my local git repo's URL to git@github.com-work:username/an-example.git

@oanhnn
Copy link
Author

oanhnn commented Aug 18, 2023

@ImadMAKS
Please using Host github.com and Host githut.com-work instead of Host git@github.com and Host git@github.com-work in your config.
After that, you need change remote URL of all repository for work.
You can use below command:

$ git remote set-url origin git@github.com-work:username/an-example.git

@thanhan2101
Copy link

Thank for your guide, it works with me.

@arpit-turing
Copy link

Thank you mate for this.

@ImadMAKS
Copy link

ImadMAKS commented Aug 25, 2023

Please using Host github.com and Host githut.com-work instead of Host git@github.com and Host git@github.com-work in your config

This worked! Thank you.

@jakubkalicki
Copy link

Great guide, it helps me a lot! And I can expand this guide with another trick.

In your ~/.gitconfig

[url "git@github.com-company:company_github_account/"]
    insteadOf = git@github.com:company_github_account/

After this setting, you don't have to change URL manually every time when you want to clone repos.

Works flawlessly!

@oanhnn, would you mind including this into your gist?

@oanhnn
Copy link
Author

oanhnn commented Aug 30, 2023

@jakubkalicki
I didn't use that way.
What will happen if I clone git@github.com-company:company_github_account/project1.git and github.com-company:other-org/project2.git after setting up in ~/.gitconfig?

@jakubkalicki
Copy link

jakubkalicki commented Aug 30, 2023

It should work as usual. What is cool about the .gitconfig thingy is that it allows for using standard url that you copy from Github repository page. It saves you from changing github.com to the custom Host name, because it will happen automatically. One less thing to remember about.

@oanhnn
Copy link
Author

oanhnn commented Aug 31, 2023

@jakubkalicki
I'm working fine with my configuration.
I'm not sure your configuration is correct in all cases, mine works fine (because it affects to SSH). I also don't have much time to compare and experiment with your configuration.
Sorry for that.
If possible, write a guide for newbies, I can link to it here.

@manzaloros
Copy link

manzaloros commented Sep 3, 2023

These instructions didn't work for me.

# Other github account: my personal account
Host <my personal alias>
   HostName github.com
   IdentityFile ~/.ssh/<my private key generated separately from my work private key>
   IdentitiesOnly yes
ssh -T git@<my personal alias>

Hi <my WORK user name>! You've successfully authenticated, but GitHub does not provide shell access.

So even though I have the personal alias defined in the ~/.ssh/config, ssh -T returns a response from GitHub that's using my work user name.

@oanhnn
Copy link
Author

oanhnn commented Sep 5, 2023

@manzaloros Can you re-run ssh -vT git@<my personal alias> and see why it is not correct?

@manzaloros
Copy link

Thanks @oanhnn . What I don't understand is why once ssh recognizes I have that personal alias it continues to try my work account credentials:

debug1: /Users/<me>/.ssh/config line 9: include ~/.ssh/config.v1.1.67 matched no files
debug1: Reading configuration data /Users/<me>/.ssh/config.custom
debug1: /Users/<me>/.ssh/config.custom line 3: Applying options for <my personal alias>

...

debug1: identity file /Users/<me>/.ssh/<my personal alias>private_key type 3
debug1: identity file /Users/<me>/.ssh/<my personal alias>private_key-cert type -1
debug1: identity file /Users/<me>/.ssh/<my work account>ssh_key type 0
debug1: identity file /Users/<me>/.ssh/<my work account>_ssh_key-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_9.0
debug1: Remote protocol version 2.0, remote software version babeld-7e018303
debug1: compat_banner: no match: babeld-7e018303
debug1: Authenticating to github.com:22 as 'git'
debug1: load_hostkeys: fopen /Users/<me>/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory

...

debug1: Will attempt key: /Users/<me>/.ssh/<my work account>_ssh_key <RSA key> explicit agent
debug1: Will attempt key: /Users/<me>/.ssh/<my personal account>private_key <ED key> explicit

...

debug1: Offering public key: /Users/<me>/.ssh/<my work account>_ssh_key <RSA key> explicit agent
debug1: Server accepts key: /Users/<me>/.ssh/<my work account>_ssh_key <RSA key> explicit agent

debug1: client_input_hostkeys: searching /Users/<me>/.ssh/known_hosts for github.com / (none)
debug1: client_input_hostkeys: searching /Users/<me>/.ssh/known_hosts2 for github.com / (none)
debug1: client_input_hostkeys: hostkeys file /Users/<me>/.ssh/known_hosts2 does not exist
debug1: client_input_hostkeys: no new or deprecated keys from server

@oanhnn
Copy link
Author

oanhnn commented Sep 6, 2023

@manzaloros
I see SSH was attempted with two keys

  • K1: /Users/<me>/.ssh/<my work account>_ssh_key <RSA key>
  • K2/Users/<me>/.ssh/<my personal account>private_key <ED key>

For some reason, both K1 and K2 match with host, but K1 was given priority over K2.
I'm not sure but it could be due to the key type and algorithms.
All algorithms involved in SSH connection, including the same process, select the host key type:

  • First the server and the client exchange lists of algorithms they support
  • Then one party (in this case the client) picks the one it prefers out of commonly supported algorithms.

You should:

  • Check all ssh_config files and included files ( include ~/.ssh/config.v1.1.67 is missing file)
  • Check all ssh_know_hosts for Github host and your alias host
  • Check HostKeyAlgorithms config

My debug

$ ssh -vT <my host alias>
OpenSSH_9.0p1, LibreSSL 3.3.6
debug1: Reading configuration data /Users/<me>/.ssh/config
debug1: Reading configuration data /Users/<me>/.colima/ssh_config
debug1: /Users/<me>/.ssh/config line 167: Applying options for <my host alias>
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: include /etc/ssh/ssh_config.d/* matched no files
debug1: /etc/ssh/ssh_config line 54: Applying options for *
debug1: Connecting to github.com port 22.
debug1: Connection established.
debug1: identity file /Users/<me>/.ssh/id_rsa type 0
debug1: identity file /Users/<me>/.ssh/id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_9.0
debug1: Remote protocol version 2.0, remote software version babeld-7e018303
debug1: compat_banner: no match: babeld-7e018303
debug1: Authenticating to github.com:22 as 'git'
debug1: load_hostkeys: fopen /Users/<me>/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU
debug1: load_hostkeys: fopen /Users/<me>/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: Host 'github.com' is known and matches the ED25519 host key.
debug1: Found key in /Users/<me>/.ssh/known_hosts:6
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: get_agent_identities: bound agent to hostkey
debug1: get_agent_identities: agent returned 3 keys
debug1: Will attempt key: /Users/<me>/.ssh/id_rsa <RSA key> explicit agent
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp256-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256,ssh-rsa>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/<me>/.ssh/id_rsa <RSA key> explicit agent
debug1: Server accepts key: /Users/<me>/.ssh/id_rsa <RSA key> explicit agent
Authenticated to github.com ([xx.xx.xx.xx]:22) using "publickey".
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: filesystem
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: client_input_hostkeys: searching /Users/<me>/.ssh/known_hosts for github.com / (none)
debug1: client_input_hostkeys: searching /Users/<me>/.ssh/known_hosts2 for github.com / (none)
debug1: client_input_hostkeys: hostkeys file /Users/<me>/.ssh/known_hosts2 does not exist
debug1: client_input_hostkeys: no new or deprecated keys from server
debug1: Sending environment.
debug1: channel 0: setting env LC_TERMINAL_VERSION = "3.4.20"
debug1: channel 0: setting env LC_CTYPE = "UTF-8"
debug1: channel 0: setting env LC_TERMINAL = "iTerm2"
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
Hi <me>! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 3532, received 3056 bytes, in 0.9 seconds
Bytes per second: sent 4068.4, received 3520.1
debug1: Exit status 1

@manzaloros
Copy link

manzaloros commented Sep 10, 2023

Thanks @oanhnn .

One note is that I didn't add an alias for my work account — just one for my personal account.

According to this discussion it looks like the search for the host is recursive and once SSH finds my hostname is github.com, it just defaults to my work settings.

Also worth noting that adding
sshCommand = ssh -i ~/.ssh/my-personal-key -F /dev/null to the repo's git config changes the error from

ERROR: Permission to <personal repo>.git denied to <my work account>.

to:

ssh: Could not resolve hostname <my personal alias>: nodename nor servname provided, or not known

@ploissken
Copy link

Finally something that works! Thank you so much!

@fons-digitect
Copy link

fons-digitect commented Sep 19, 2023

@oanhnn It is worth specifically mentioning that with step 4, the file should literally be called "config" without any extension. Otherwise testing the connection will fail, because your config file isn't recognized by ssh. Check out the green answer at: https://github.com/orgs/community/discussions/22589

For the rest this article helped, thanks!

@awais305
Copy link

awais305 commented Oct 21, 2023

Thanks. it's really helpful.
I have a question about the bash script. Could you please clarify where it's supposed to be added and then use it? I'm a bit confused about that part.

@oanhnn
Copy link
Author

oanhnn commented Oct 23, 2023

@fons-digitect
The file naming is very clear.
Everyone does exactly that.

@awais305
You can save the bash script anywhere and call it by /path/to/bash-script.sh.
You should save to /usr/local/bin/git-profile (or anywhere in $PATH) for calling shorter (only git-profile)

@iskode
Copy link

iskode commented Nov 6, 2023

Thank you so much... I've been unable to work collaboratively for 3 weeks !
Finally I can resume.

@shagha-macrometa
Copy link

Thank you for sharing this!! Such a simple and clever solution! Saved me the trouble of reading through git config docs!

@mihokpeter
Copy link

Hi there! Are any of you guys using this setup also using Fork as a git client or GUI for git? I am asking this, because I have some problems with fetching/pull/pushing my personal repo.

Here's my setup of .ssh/config file:

Work github account: peter.mihok-work

Host github.com
HostName github.com
IdentityFile ~/.ssh/id_work
IdentitiesOnly yes
UseKeychain yes
AddKeysToAgent yes
ControlMaster no

Personal github account: mihokpeter

Host github.com-mihokpeter
HostName github.com
IdentityFile ~/.ssh/id_mihokpeter
IdentitiesOnly yes
UseKeychain yes
AddKeysToAgent yes
ControlMaster no

What I don't understand is that even-thought my remote origin url is set either git@github.com:something/file.git or git@github.com-mihokpeter:mihokpeter/file.git. In my personal repo I also added git config user.email and git config user.name. While using Fork I can easily push/pull/etc.. on my work branch, but I can't do none of this on my personal one. However, I can do all of those in terminal in my personal branch. So, with that said I think my ssh must be setup correctly (as those git commands are working) + I can see correct names being printed out in terminal after running ssh -T git@github.com-mihokpeter and ssh -T git@github.com.

Any ideas what can be wrong? I am really struggling here and I would appreciate any help!

@oanhnn
Copy link
Author

oanhnn commented Dec 15, 2023

@mihokpeter
You should ensure that your repository's remote origin URL is

$ git remote -v
origin git@github.com-mihokpeter:mihokpeter/file.git (fetch)
origin git@github.com-mihokpeter:mihokpeter/file.git (push)

Please look at the 2nd dot of step 5 in this gist.

@y-he2
Copy link

y-he2 commented Dec 22, 2023

This is super! However I couldnt get the "ssh -T" verify step to work without adding:

Host git@...
        ...
	User git
	...

So something to be awared maybe.

@oanhnn
Copy link
Author

oanhnn commented Dec 22, 2023

@y-he2 Can you show debug log (ssh -T git@github-alias) in case Host github-alias?

@y-he2
Copy link

y-he2 commented Dec 22, 2023

@y-he2 Can you show debug log (ssh -T git@github-alias) in case Host github-alias?

Heres what I could dig up from log with ChatGPT:
(Without "User git") It seems with "ssh -v HostName" even the SHA is matching, but it just give:
...
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
my_os_username @ github.com: Permission denied (publickey).

@oanhnn
Copy link
Author

oanhnn commented Dec 23, 2023

@y-he2
In your case, ssh will use a username from the logged-in user in the shell.

Host github-alias
   HostName github.com
   IdentityFile ~/.ssh/account1_private_key
   IdentitiesOnly yes
oanhnn@localhost:~$ ssh -vT github-alias
oanhnn@localhost:~$ ssh -vT oanhnn@github.com

Two commands have the same effect.
You should set it up one of two ways.

1 (your solution)

Host github-alias
   HostName github.com
   User git
   IdentityFile ~/.ssh/account1_private_key
   IdentitiesOnly yes
ssh -vT github-alias

2 (like my guide):

Host github-alias
   HostName github.com
   IdentityFile ~/.ssh/account1_private_key
   IdentitiesOnly yes
ssh -vT git@github-alias

@y-he2
Copy link

y-he2 commented Dec 24, 2023

@y-he2 In your case, ssh will use a username from the logged-in user in the shell.

Host github-alias
   HostName github.com
   IdentityFile ~/.ssh/account1_private_key
   IdentitiesOnly yes
oanhnn@localhost:~$ ssh -vT github-alias
oanhnn@localhost:~$ ssh -vT oanhnn@github.com

Two commands have the same effect. You should set it up one of two ways.

1 (your solution)

Host github-alias
   HostName github.com
   User git
   IdentityFile ~/.ssh/account1_private_key
   IdentitiesOnly yes
ssh -vT github-alias

2 (like my guide):

Host github-alias
   HostName github.com
   IdentityFile ~/.ssh/account1_private_key
   IdentitiesOnly yes
ssh -vT git@github-alias

Ah that "git@" is obviously what I have been missing, thanks for the clarification!

@oanhnn
Copy link
Author

oanhnn commented Jan 12, 2024

@viktorianer
Copy link

👍 That solution is good. But the only issue is that typing that command is somewhat long and tiring.

That is nota problem at all. Just add an alias for this command, a short one! ☺️

@gentunian
Copy link

problem with this is that only allows you to use your keys for cloning. But it won't work for installing dependencies that points to ssh@github.com/some/repo.

@awaisalwaisy
Copy link

Thanks bunch it worked for me. Saved my time. Stay happy and posting amazing stuff.

@smali-kazmi
Copy link

please add one thing in your gist suppose there is an github-superman account so you have to update your package.json like this

{
  "dependencies": {
      ......
      ......
      testRepo: "git+ssh://git@github-superman:org/testRepo.git#main"
  }
}

@IbroRebronja
Copy link

Very useful!

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