2

I'm struggling with git/SSH multiple identities (one personal/work and one dedicated to yet another company)

I've managed to somehow create a virtual local "routing" thanks to https://simonbasle.github.io/2017/10/git-identities-and-ssh/, which selects the right SSH key depending on where the local repo is located on the disk. That's a first good step.

But I'm struggling to adapt my "gh" commands:

gh repo create REPO_NAME \
--template USER_NAME/TEMPLATE_NAME \
--private \
--clone

Doing this somehow successfully creates the REPO_NAME repository in the USER_NAME account, but then it fails at the "clone" step, with ERROR: Repository not found..

I believe that's because gh doesn't use the right SSH key, but I don't understand how to specify which one to choose from.

4
  • I'm no expert on gh but my understanding is that when it creates a repository, it uses the GitHub v3 API via https (only), even if it later uses ssh to clone the created repository. You might be best off using gh to create it, and then git clone with the adjusted URL as in the article you referenced. That is, your gh repo create is not using ssh. By doing git clone yourself, rather than with gh, you can control the URL you supply. Commented Dec 4, 2022 at 8:24
  • 1
    Still, there may be a way to get the --clone option to work. I adjusted your tags to reach gh experts. Commented Dec 4, 2022 at 8:28
  • I just found stackoverflow.com/questions/74644191/… which seems to say "this isn't yet supported". Commented Dec 4, 2022 at 10:17
  • This question is similar to: How do I tell the GitHub CLI to use a specific SSH key?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Nov 4, 2024 at 17:00

2 Answers 2

0

This gist helped me solved this problem(before that, talked to AI for a while, chaos info provided, wasting time), hope it can help you as well(which should):

https://gist.github.com/yermulnik/017837c01879ed3c7489cc7cf749ae47

Let me summarize what I did:

Get tokens for two(or more) github accounts, visit: https://github.com/settings/tokens

Choose Personal access tokens (classic), select repo, read org, delete repo, no expiration.

cd gh config folder and open with vs code:

cd ~/.config/gh

modify config.yml, set git_protocol: ssh(if you use ssh keys), and add alias:

personal: "!cp ~/.config/gh/hosts.yml.personal ~/.config/gh/hosts.yml && gh auth status"
work: "!cp ~/.config/gh/hosts.yml.work ~/.config/gh/hosts.yml && gh auth status"

create two files:

hosts.yml.personal

content:

github.com:
oauth_token: ghp_xxx
git_protocol: ssh
user: user1

2nd file:

hosts.yml.work

content:

github.com:
oauth_token: ghp_xxx
git_protocol: ssh
user: user2

Now you can switch by:

gh personal

and

gh work

Try this:

gh repo list

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

0

You can use gh auth switch since December 18, 2023.

Note: This is still manual but the old issue is being repurposed to add some automatic switching to the above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.