0

I have various GitHub accounts and for each account I have SSH set up. So under ~/.ssh I have a public and private key for each account.

I want to use the GitHub CLI, but I am not sure how I can tell the CLI to use a particular SSH key.

In case it is relevant, this is what I get when I run ssh-add -l:

enter image description here

Example Scenario

I want to run gh repo create on GitHub account B, but for some reason, the repo got created on GitHub account A. Is there a way I can tell gh what account to use?

torek
  • 448,244
  • 59
  • 642
  • 775
J86
  • 14,345
  • 47
  • 130
  • 228
  • Does Github CLI (`gh`) uses ssh protocol at all? Do you mean `git` command line? Git is not Github CLI but can be used with Github. – phd Dec 01 '22 at 15:47
  • Yeah, you can make it use ssh via `git config set`. Yep I'm talking about `gh` (the CLI, I know Git and the GitHub CLI are two different things). – J86 Dec 01 '22 at 15:56
  • 2
    If you mean `gh config set git_protocol ssh` — that's not about Github CLI, it's the protocol to use for **git clone and push** operations. See https://cli.github.com/manual/gh_config. So the question remains: are you talking about using `gh` with SSH or `git`? – phd Dec 01 '22 at 16:06
  • Good catch! I'm still talking about `gh` though. – J86 Dec 01 '22 at 21:04
  • 2
    I don't think `gh repo create` runs over SSH. It's a [Github API](https://docs.github.com/ru/rest) call so it runs over HTTPS. You need [`gh auth logout`](https://cli.github.com/manual/gh_auth_logout) and [`gh auth login`](https://cli.github.com/manual/gh_auth_login). – phd Dec 01 '22 at 21:31

1 Answers1

1

if you have different GitHub users the gh-cli won't be very effective. as @phd commands like gh repo create require logging in via an auth token. https://cli.github.com/manual/gh_auth_login

Switching contexts between accounts (i.e. github.com/user1 and github.com/user2) def doesn't seem supported so you'd have to hack around loging in and out each time every time you switched.

But configuring which ssh key git should use can be configured easily enough in some combo of ~/.ssh/config .gitconfig and/or setting the GIT_SSH_COMMAND env var before running git commands.

deric4
  • 1,095
  • 7
  • 11