我必须采取哪些步骤才能使用 SSH 持续推送到两个 Github 帐户? [英] What steps must I take to consistently push to two Github accounts using SSH?
问题描述
我承诺将 Github 用于工作和个人用途.在过去的两个月里,每次切换 github 上下文时,我总是遇到麻烦.每天晚上(和每天早上)我都会遇到这样的错误:推入或拉出 Github:
I commit to Github for work and for personal use. For the last two months I have consistently had trouble each and every time I switch github contexts. Every night (and every morning) I run into error's pushing to or pulling form Github like this:
ERROR: Permission to AlexanderBollbach/test.git denied to alexbollbach.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
如果我能让推/拉正常工作(通常是通过一些迂回的方式阅读关于 SSH/Github/Git 的 StackOverflow 帖子,但没有深入了解它们),我在切换时立即再次遇到同样的问题从工作 Github 到家庭,反之亦然.
If I can get the pushing/pulling to work properly (usually through some circuitous path of reading StackOverflow posts about SSH/Github/Git and not understanding them in any depth), I immediately run into the same problems again when I switch from work Github to home or vice versa.
我注意到的唯一有趣的线索是,在 denied to alexbollbach
中,alexbollbach"是我的工作 Github 帐户用户名.嗯,这让我想到了~/.ssh/config
,其内容是:
The only interesting clue I have noticed is that in denied to alexbollbach
, "alexbollbach" is my work Github account username. hmm, this leads me to consider ~/.ssh/config
, whose contents are:
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
Host github.com
User git
Host helper
HostName <work-domain-stuff>
IdentityFile ~/.ssh/eng_rsa
User eng
有人告诉我这个文件有助于配置 git 的 ssh 尝试.但是我仍然无法在这个文件和 git push 错误之间建立联系,认为我是alexbollbach
.事实上,我不知道 ssh、我的密钥对和配置文件在任何意义上与 Github 用户的概念有何关联.
I was told that this file is instrumental in configuring git's ssh attempts. But I still cannot make the connection between this file and the git push error thinking that I am alexbollbach
. In fact, I do not know how ssh, my key pairs, and the config file relate to the notion of Github user's in any sense.
请帮忙.最好不仅仅是修复,而是对我没有掌握的内容进行一些澄清.我通常每周至少两次花一个小时来解决这个问题.
Please help. Preferably not just a fix but some clarification on what i'm not grasping. I routinely spend an hour at least twice a week running into this issue.
推荐答案
在 Git 中链接远程仓库和不同 ssh 密钥的方式是通过远程 url.
像这样更改这些网址:
The way you link a remote repo and different ssh keys in Git is through the remote url.
Change those urls like so:
cd /path/to/my/work/repo
git remote set-url origin workgh:myWorkLogin/myWorkrepo.git
cd /path/to/my/perso/repo
git remote set-url origin persgh:myPersoLogin/myPersorepo.git
请参阅如何从已将 SSH 密钥添加到工作相关 GitHub 帐户的办公室计算机上处理个人 GitHub 存储库?"作为一个完整的例子.
您的 ~/.ssh/config
将为每个 url 引用正确的 ssh 密钥:
See "How to work on personal GitHub repo from office computer whose SSH key is already added to a work related GitHub account?" as a full example.
Your ~/.ssh/config
will reference the right ssh key for each url:
# Personal GitHub
Host persgh
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_perso
# Work GitHub
Host workgh
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
不要使用github.com
"作为主机":使用特殊名称作为主机键更明确,并表明这是一个 ssh url,您必须通过 <代码>~/.ssh/config 文件.
Don't use "github.com
" as a "Host": using an special name for the Host key is more explicit and indicates this is an ssh url you have to resolve through a ~/.ssh/config
file.
这篇关于我必须采取哪些步骤才能使用 SSH 持续推送到两个 Github 帐户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!