试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。

工作账户是先建立的,一切都完美无缺。

然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。

我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。

我如何用各自的GitHub凭证推/拉两个帐户?


当前回答

相反,您只需将新的电子邮件id添加到您的个人git帐户。这样您就不需要添加另一个SSH密钥。只需按以下步骤配置新邮箱即可 Git配置——全局用户。电子邮件newemail。然后你就可以复制那个回购了。

其他回答

最简单和直接的方法(IMHO) -没有配置文件,没有太多的麻烦

只需创建另一个ssh密钥。

假设你有一个新的GitHub工作帐户,只需为它创建一个新密钥:

ssh-keygen -t rsa -C "email@work_mail.com" -f "id_rsa_work_user1"`

您只需要运行一次上面的程序。

现在你应该有了旧的和新的,要查看它们,运行:

ls -al ~/.ssh

从现在开始,每次你想在两者之间切换时,只需运行:

ssh-add -D
ssh-add ~/.ssh/id_rsa_work_user1 #make to use this without the suffix .pub

为了切换到旧的,再次运行:

 ssh-add -D
 ssh-add ~/.ssh/<previous id_rsa>

更简单和容易修复,以避免混乱..

Windows用户可以为不同的项目使用多个或不同的git帐户。

以下步骤: 进入控制面板,搜索凭据管理器。 然后转到凭证管理器-> Windows凭证

现在在Generic Credentials Heading下删除git:https//github.com节点

这将删除当前凭据。现在你可以通过git添加任何项目,它会要求用户名和密码。

当你遇到任何问题与其他帐户做同样的过程。

塑形

要在单独的github/bitbucket/任何帐户下管理git回购,你只需要生成一个新的SSH密钥。

但在我们开始用你的第二个身份推/拉回购之前,我们必须让你进入状态-让我们假设你的系统是用典型的id_rsa和id_rsa设置的。Pub密钥对。现在你的树~/。SSH是这样的

$ tree ~/.ssh
/Users/you/.ssh
├── known_hosts
├── id_rsa
└── id_rsa.pub

首先,命名该密钥对——添加一个描述性的名称将帮助您记住哪个密钥用于哪个用户/远程

# change to your ~/.ssh directory
$ cd ~/.ssh

# rename the private key
$ mv id_rsa github-mainuser

# rename the public key
$ mv id_rsa.pub github-mainuser.pub

接下来,让我们生成一个新的密钥对——这里我将新密钥命名为github-otheruser

$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/github-otheruser

现在,当我们看树~/。SSH我们看到了

$ tree ~/.ssh
/Users/you/.ssh
├── known_hosts
├── github-mainuser
├── github-mainuser.pub
├── github-otheruser
└── github-otheruser.pub    

接下来,我们需要设置一个~/。Ssh /config文件将定义我们的关键配置。我们将使用适当的所有者只读/只写权限创建它

$ (umask 077; touch ~/.ssh/config)

用你最喜欢的编辑器打开它,并添加以下内容

Host github.com
  User git
  IdentityFile ~/.ssh/github-mainuser

Host github.com-otheruser
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-otheruser

假设,你会有一些现有的回购与你的主要github身份相关联。因此,“默认”github.com主机设置为使用mainuser密钥。如果您不想偏爱某个帐户而不是另一个帐户,我将向您展示如何更新系统上现有的回购以使用更新后的ssh配置。


将新的SSH密钥添加到github

转到github.com/settings/keys,添加新的公钥

你可以使用:复制/粘贴到github来获取公钥内容

$ cat ~/.ssh/github-otheruser.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDBVvWNQ2nO5...

现在您的新用户身份已经全部设置完毕-下面我们将向您展示如何使用它。


完成工作:克隆回购

那么这是如何一起工作与git和github?因为没有蛋就没有鸡,所以我们将研究克隆现有的回购。如果你的工作场所有一个新的github帐户,并且你被添加到一个公司项目中,这种情况可能适用于你。

假设github.com/someorg/somerepo已经存在,你被添加到它-克隆是一样容易

$ git clone github.com-otheruser:someorg/somerepo.git

粗体部分必须与我们在~/中设置的主机名匹配。ssh / config文件。正确地将git连接到相应的IdentityFile,并使用github正确地验证您的身份


完成工作:创建一个新的回购

好吧,因为你不能没有鸡和蛋,我们会考虑在你的二级账户上发布一个新的回购。这种情况适用于使用其次要github帐户创建新内容的用户。

让我们假设你已经在本地做了一些工作,现在准备推送到github。如果你愿意,你可以跟我一起走

$ cd ~
$ mkdir somerepo
$ cd somerepo
$ git init

现在配置这个回购使用您的身份

$ git config user.name "Mister Manager"
$ git config user.email "someuser@some.org"

现在做出你的第一个承诺

$ echo "hello world" > readme
$ git add .
$ git commit -m "first commit"

检查提交,看看你的新身份是使用git日志

$ git log --pretty="%H %an <%ae>"
f397a7cfbf55d44ffdf87aa24974f0a5001e1921 Mister Manager <someuser@some.org>

好了,该推到github了!因为github还不知道我们的新回购,首先去github.com/new并创建您的新回购-命名为somerepo

现在,为了配置你的repo使用正确的身份/凭据与github“对话”,我们已经添加了一个遥控器。假设你的github用户名为你的新帐户是someuser…

$ git remote add origin github.com-otheruser:someuser/somerepo.git

粗体部分非常重要,它必须与我们在~/中定义的Host相匹配。ssh / config文件

最后,推动回购

$ git push origin master

更新现有的repo以使用新的SSH配置

假设您已经克隆了一些回购,但现在想使用新的SSH配置。在上面的例子中,我们通过分配你之前的id_rsa/id_rsa来保持你现有的回购。pub密钥对到主机github.com在您的SSH配置文件。这没有什么问题,但我现在至少有5个github配置,我不喜欢把它们中的一个视为“默认”配置-我宁愿明确地说明每一个。

在这之前

Host github.com
  User git
  IdentityFile ~/.ssh/github-mainuser

Host github.com-otheruser
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-otheruser

现在我们将它更新为this(粗体部分的改动)

Host github.com-mainuser
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-mainuser

Host github.com-otheruser
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-otheruser

但现在任何现有的回购与github.com远程将无法与此身份文件工作。但别担心,这是一个简单的解决办法。

要更新任何现有的repo以使用新的SSH配置,请使用set-url -更新repo的远程原点字段

$ cd existingrepo
$ git remote set-url origin github.com-mainuser:someuser/existingrepo.git

就是这样。现在你可以尽情地推/拉了


SSH密钥文件权限

如果您遇到了公钥不能正常工作的问题,SSH对~/上允许的文件权限非常严格。SSH目录和相应的密钥文件

根据经验,任何目录都应该是700,任何文件都应该是600——这意味着它们是所有者只读/只读的——没有其他组/用户可以读/写它们

$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/config
$ chmod 600 ~/.ssh/github-mainuser
$ chmod 600 ~/.ssh/github-mainuser.pub
$ chmod 600 ~/.ssh/github-otheruser
$ chmod 600 ~/.ssh/github-otheruser.pub

如何管理SSH密钥

我为我连接到的每个主机管理单独的SSH密钥,这样,如果任何一个密钥被泄露,我不必在我使用该密钥的每个其他地方更新密钥。这就像你收到Adobe的通知,说他们有1.5亿用户的信息被盗了——现在你必须取消那张信用卡,并更新所有依赖于它的服务——多么讨厌。

这是我的~/。ssh目录如下:每个用户都有一个.pem密钥,在连接到的每个域的文件夹中。我使用.pem键,所以每个键只需要一个文件。

$ tree ~/.ssh
/Users/myuser/.ssh
├── another.site
│   ├── myuser.pem
├── config
├── github.com
│   ├── myuser.pem
│   ├── someusername.pem
├── known_hosts
├── somedomain.com
│   ├── someuser.pem
└── someotherdomain.org
     └── root.pem

这是我对应的/。Ssh /配置文件——显然,github的东西与回答关于github的这个问题有关,但这个答案旨在让你具备在任何数量的服务/机器上管理Ssh身份的知识。

Host another.site
  User muyuser
  IdentityFile ~/.ssh/another.site/muyuser.pem

Host github.com-myuser
  HostName github.com
  User git
  IdentityFile ~/.ssh/github.com/myuser.pem

Host github.com-someuser
  HostName github.com
  User git
  IdentityFile ~/.ssh/github.com/someusername.pem

Host somedomain.com
  HostName 162.10.20.30
  User someuser
  IdentityFile ~/.ssh/somedomain.com/someuser.pem

Host someotherdomain.org
  User someuser
  IdentityFile ~/.ssh/someotherdomain.org/root.pem

从PEM密钥中获取SSH公钥

上面您注意到,每个键只有一个文件。当我需要提供公钥时,我只需根据需要生成它。

因此,当github要求您的ssh公钥时,运行此命令将公钥输出到标准输出-在需要的地方复制/粘贴

$ ssh-keygen -y -f someuser.pem
ssh-rsa AAAAB3NzaC1yc2EAAAA...

注意,这也是我用于向任何远程机器添加密钥的相同过程。ssh-rsa AAAA…值复制到远程的~/。ssh / authorized_keys文件


转换您的id_rsa/id_rsa。pub密钥对为PEM格式

所以你想驯服你的密钥文件,减少一些文件系统的麻烦?将密钥对转换为单个PEM非常简单

$ cd ~/.ssh
$ openssl rsa -in id_rsa -outform pem > id_rsa.pem

或者,按照上面的例子,我们重命名id_rsa -> github-mainuser和id_rsa。Pub -> github-mainuser。酒吧- so

$ cd ~/.ssh
$ openssl rsa -in github-mainuser -outform pem > github-mainuser.pem

现在,为了确保转换正确,您需要验证生成的公钥是否与旧的公钥匹配

# display the public key
$ cat github-mainuser.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAA ... R++Nu+wDj7tCQ==

# generate public key from your new PEM
$ ssh-keygen -y -f someuser.pem
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAA ... R++Nu+wDj7tCQ==

现在你有了你的github-mainuser。Pem文件,您可以安全地删除旧的github-mainuser和github-mainuser。pub文件-只需要PEM文件;只要在你需要的时候生成公钥^_^


从头开始创建PEM密钥

您不需要创建私钥/公钥对,然后再转换为单个PEM密钥。可以直接创建PEM密钥。

让我们创建一个newuser.pem

$ openssl genrsa -out ~/.ssh/newuser.pem 4096

SSH公钥的获取方法相同

$ ssh-keygen -y -f ~/.ssh/newuser.pem
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACA ... FUNZvoKPRQ==

如果您已经创建或克隆了另一个存储库,并且无法从原点或上游提取,则使用以下命令在该目录中添加ssh密钥是有效的。

这是我得到的错误:

Warning: Permanently added the RSA host key for IP address '61:fd9b::8c52:7203' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

我使用了下面的命令,这是有效的:

ssh-add ~ / . ssh / id_rsa_YOUR_COMPANY_NAME

这个答案是给初学者的(非git大师)。我最近遇到了这个问题,也许这只是我的问题,但大多数答案似乎需要对git有相当深入的了解。在阅读了包括这个线程在内的几个堆栈溢出的答案后,下面是我需要采取的步骤,以便在GitHub帐户之间轻松切换(例如,假设有两个GitHub帐户,github.com/personal和gitHub.com/work):

Check for existing ssh keys: Open Terminal and run this command to see/list existing ssh keys ls -al ~/.ssh files with extension .pub are your ssh keys so you should have two for the personal and work accounts. If there is only one or none, its time to generate other wise skip this.- Generating ssh key: login to github (either the personal or work acc.), navigate to Settings and copy the associated email.now go back to Terminal and run ssh-keygen -t rsa -C "the copied email", you'll see:Generating public/private rsa key pair. Enter file in which to save the key (/.../.ssh/id_rsa): id_rsa is the default name for the soon to be generated ssh key so copy the path and rename the default, e.g. /.../.ssh/id_rsa_work if generating for work account. provide a password or just enter to ignore and, you'll read something like The key's randomart image is: and the image. done.Repeat this step once more for your second github account. Make sure you use the right email address and a different ssh key name (e.g. id_rsa_personal) to avoid overwriting. At this stage, you should see two ssh keys when running ls -al ~/.ssh again. Associate ssh key with gitHub account: Next step is to copy one of the ssh keys, run this but replacing your own ssh key name: pbcopy < ~/.ssh/id_rsa_work.pub, replace id_rsa_work.pub with what you called yours.Now that our ssh key is copied to clipboard, go back to github account [Make sure you're logged in to work account if the ssh key you copied is id_rsa_work] and navigate toSettings - SSH and GPG Keys and click on New SSH key button (not New GPG key btw :D) give some title for this key, paste the key and click on Add SSH key. You've now either successfully added the ssh key or noticed it has been there all along which is fine (or you got an error because you selected New GPG key instead of New SSH key :D). Associate ssh key with gitHub account: Repeat the above step for your second account. Edit the global git configuration: Last step is to make sure the global configuration file is aware of all github accounts (so to say). Run git config --global --edit to edit this global file, if this opens vim and you don't know how to use it, press i to enter Insert mode, edit the file as below, and press esc followed by :wq to exit insert mode: [inside this square brackets give a name to the followed acc.] name = github_username email = github_emailaddress [any other name] name = github_username email = github_email [credential] helper = osxkeychain useHttpPath = true

完成了!现在,当尝试从一个回购中推或拉,你会被问到哪个GitHub账户应该与这个回购链接,它只被问到一次,本地配置将记住这个链接,而不是全局配置,所以你可以在不同的回购上工作,链接到不同的账户,而不必每次编辑全局配置。