我需要在我的本地机器上使用我公司的 GitHub。
我更喜欢使用 SSH。
我有一个私有(private) GitHub 帐户,该帐户已使用本地默认 key (~/.ssh/id_rsa.pub) 使用 SSH。
我使用 Windows。
我使用我的公司帐户登录,我可以看到存储库。
如果我这样做 git clone <git url>
或 git clone <https url>
我收到:“找不到存储库”。
与 git -T git@github.com
我看到我已通过我的个人 GitHub 帐户获得授权。
我尝试在公司 GitHub 帐户上注册本地默认 SSH key (id_rsa.pub),但它说该 key 已在使用中(我想它指的是我的个人帐户)。
我注册了另一个 key (id_rsa_test.pub) 但我在 git 中找不到任何参数来传递不同的 key /凭据?
如何在本地 PC 上通过 SSH 使用 git 而不用特定的 SSH key 替换我的个人 SSH key ?
作为临时解决方案,我尝试将其放入 GIT 配置中,但不起作用(仍然是“未找到存储库”错误)
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>
返回“连接到代理时出错:没有这样的文件或目录”
我想是因为在这台机器上我没有安装 Git-Bash。
(据我所知,实际上必须始终启动并运行 ssh-agent?)
GIT_SSH_COMMAND='ssh -i C:\Users\alex\.ssh\new_key_rsa'
返回“'GIT_SSH_COMMAND' 未被识别为内部或外部命令”
[临时解决方案:GitHub Desktop]
作为临时解决方案,我正在使用 GitHub Desktop。
它始终使用浏览器进行身份验证(也是在您通过用户/密码并输入通过 SMS 收到的验证码后!!)并且它始终使用浏览器中已登录的用户。
于是,我输入我公司账号的用户名和密码,点击“登录”,收到验证码短信,OK。它说我必须使用浏览器进行身份验证,点击“继续”...它会打开浏览器,我会自动使用我的个人帐户进行身份验证!
所以,我必须从我的个人帐户退出浏览器,在浏览器中使用我的公司帐户登录后重复该过程。
(我为此开了一个 Issue)
它允许我从列表中选择要克隆的存储库。我克隆了它,发现它使用的是 HTTPS URL (repo/.git//config):
[remote "origin"]
url = https://github.com/<my-company-repo>.git
(也许我可以将其更改为 SSH,我会尝试) 所以,我希望这个解决方案不需要我经常在浏览器中切换用户。我仍然想知道如何直接使用 git(没有第 3 方工具)。
最佳答案
As a temporary solution I tried to put this in the GIT config, but does not work
要使用该配置,您需要:
- 将用户更改为
git
(与 GitHub 的 SSH 连接总是使用“git”:私钥将对实际用户进行身份验证) - 更改您的网址
所以:
Host GitHub
HostName github.com
User git <<=== not alex!
IdentityFile c:\Users\alex\.ssh\new_key_rsa
和:
cd C:\path\to\repo
git remote set-url GitHub:<me>/<myrepo>
关键部分是否使用与git@github.com
关联的正确私钥
这里的配置条目被命名为GitHub
(但它可以命名为 xxx
)。
这就是为什么 URL 必须变成 GitHub:<company>/<comany-repo>.git
的原因为了使用那个~/.ssh/config
条目“GitHub
”。
您可以为同一个 git@github.com 定义许多条目,正如我 8 年前在“How to work on personal GitHub repo from office computer whose SSH key is already added to a work related GitHub account?”中所说明的那样
关于使用非默认 SSH key 的 git clone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62047172/