3

I need to use my company GitHub from my local machine.
I prefer to use SSH.

I have a private GitHub account that is already using SSH with the local default key (~/.ssh/id_rsa.pub).
I use Windows.

I'm logged in with my company account and I can see the repositories.
If I do git clone <git url> or git clone <https url> I receive: "repository not found".

With git -T git@github.com I see that I'm autenticated with my personal GitHub account.

I tried to regiter the local default SSH key (id_rsa.pub) on the company GitHub account but it says the key is already in use (I imagine it refers to my personal account).
I registered another key (id_rsa_test.pub) but I can't find any parameter in git to pass a different key/credentials?

How can I use git, with SSH, from my local PC without replacing my personal SSH key with a specific SSH key?

As a temporary solution I tried to put this in the GIT config, but does not work (still "Repository not found" error)

Host GitHub
    HostName github.com
    User alex-company-user
    IdentityFile c:\Users\alex\.ssh\new_key_rsa

ssh-add <path to the new key file> returns "Error connecting to agent: No such file or directory" I think because on this machine I don't have Git-Bash installed. (as I understood it is practically mandatory to have a up and running ssh-agent all the time?)

GIT_SSH_COMMAND='ssh -i C:\Users\alex\.ssh\new_key_rsa' returns "'GIT_SSH_COMMAND' is not recognized as an internal or external command"

[Temporary solution: GitHub Desktop]

As a temporary solution I'm usaing GitHub Desktop.
It ALWAYS authenticate with the browser (also after you passed the user/password and entered the verification code received by SMS !!) and it ALWAYS use the alredy logged in user in the browser.
So, I enter the username and password of my company account, click "sign in", receive the SMS with the verification code, OK. It says I have to authenticate with the browser, click "continue"... it opens the browser and I'm automatically authenticated with my personal account!
So, I have to log out on the browser from my personal account, repeat the procedure after been logged in with my company account in the browser.
(I opened an Issue for this)
It lets me choose the repository to clone from a list. I cloned it and I found it is using the HTTPS URL (repo/.git//config):

[remote "origin"]
    url = https://github.com/<my-company-repo>.git

(maybe I can change this to the SSH, I'll try) So, I hope this solution does not require me to switch the user in the browser often. I still like to know how to do it using git directly (without a 3rd party tool).

Alex 75
  • 2,798
  • 1
  • 31
  • 48
  • There's no real reason to reuse your personal key with your company account. Just generate a new pair. – chepner May 28 '20 at 00:52
  • The problem is that when I do git clone that repo is not visible because GIT does not allow to pass the "other" SSH key. That's what I think is the reason for the "Repository not found error". My company repo is obviously private. – Alex 75 May 28 '20 at 09:02

1 Answers1

1

As a temporary solution I tried to put this in the GIT config, but does not work

To use that configuration, you would need to:

  • change the user to git (an SSH connection to GitHub always uses 'git': the private key will authenticate the actual user)
  • change your URL

So:

Host GitHub
    HostName github.com
    User git <<=== not alex!
    IdentityFile c:\Users\alex\.ssh\new_key_rsa

And:

cd C:\path\to\repo
git remote set-url GitHub:<me>/<myrepo>

The key part if to use the rigth private key in association to git@github.com

Here the config entry is named GitHub (but it could be named xxx).
That is why the URL must become GitHub:<company>/<comany-repo>.git in order to use that ~/.ssh/config entry "GitHub".

You can define many entries for the same git@github.com, as I illustrated 8 years ago in "How to work on personal GitHub repo from office computer whose SSH key is already added to a work related GitHub account?"

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I corrected the user. I already tried with _git_, I was indecise about **my-user** or **git** because for GitLab it is set with **my-user**. I'm using this URL: git@github.com:.git . I'm not changing a repository from HTTPS to SSH, I need to clone it for the first time. Still have "Repository not found" when call ``git clone git@github.com:.git``. (same error with the HTTPS url too). – Alex 75 May 28 '20 at 09:12
  • @Alex75 That seems normal: an actual GitHub URL involves a user or organization, followed by a repository: `git@github.com:/.git` – VonC May 28 '20 at 09:28
  • @Alex75 Check first with `ssh -Tv GitHub` if you are correctly authenticated – VonC May 28 '20 at 09:30
  • PS D:\temp> ssh -Tv GitHub OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.5 debug1: Reading configuration data C:\\Users\\alex/.ssh/config debug1: C:\\Users\\alex/.ssh/config line 10: Applying options for GitHub [cut] debug1: identity file c:\\Users\\alex\\.ssh\\ type 0 debug1: key_load_public: No such file or directory debug1: identity file c:\\Users\\alex\\.ssh\\-cert type -1 debug1: Local version string SSH-2.0-OpenSSH_for_Windows_7.7 debug1: Remote protocol version 2.0, remote software version babeld-f24cc9ea debug1: no match: babeld-f24cc9ea [continue...] – Alex 75 May 28 '20 at 11:11
  • @Alex75 As long as `ssh -Tv GitHub` (with "`GitHub`" being the Host entry name in your `~/.ssh/config` file ) does not end up with "`Welcome xxx`", nothing else will work. – VonC May 28 '20 at 11:13
  • [...continue] debug1: Authenticating to github.com:22 as 'git' [cut] debug1: Found key in C:\\Users\\alex/.ssh/known_hosts:1 [cut] debug1: Offering public key: RSA SHA256:******* c:\\Users\\alex\\.ssh\\ debug1: Server accepts key: pkalg ssh-rsa blen 535 debug1: Authentication succeeded (publickey). Authenticated to github.com ([140.82.118.3]:22). [cut] Hi ! You've successfully authenticated, but GitHub does not provide shell access. Yes... it ends up with my company user, so the config did the work. – Alex 75 May 28 '20 at 11:14
  • @Alex75 Great, so a URL like `GitHub:/.git` should too (not `git@github.com:...` but `GitHub:...`) – VonC May 28 '20 at 11:15
  • but still: PS D:\temp> git clone git@github.com:.git Cloning into '... ERROR: Repository not found. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. The url is correct, it it the one on the GitHub page. – Alex 75 May 28 '20 at 11:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/214818/discussion-between-vonc-and-alex-75). – VonC May 28 '20 at 11:15