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

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

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

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

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


当前回答

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

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

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

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

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

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

其他回答

如果你碰巧安装了WSL,你可以有两个独立的git帐户——一个在WSL上,一个在windows上。

我看到这里有很多可行的变通办法。作为权宜之计,@Greg的方法似乎相对简单。但是,从长远来看,最好为所有不同的帐户设置单独的ssh密钥。这个视频简单演示了一下。以下是视频博客中提到的步骤。

步骤1 -为新帐户创建一个新的SSH密钥,并将其保存在一个单独的文件中(例如~/. SSH /id_rsa_new_account_name),而不是在原来的文件中,例如~/. SSH /id_rsa

ssh-keygen -t rsa -C "your-email-address"

步骤2 -附加新密钥

接下来,登录到你的第二个GitHub账户 浏览到Account Overview,并附加新密钥~/.ssh/id_rsa_new_account_name。在SSH Public Keys部分中。 在终端中,通过键入SSH -add ~/. SSH /id_rsa_new_account_name告诉SSH添加新的标识。如果成功,您将看到Identity Added的响应。

步骤3 -创建配置文件

touch ~/.ssh/config

并将以下内容保存到文件中

#Default GitHub
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

#New Account
Host github-new-account-name
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_new_account_name

第四步——尝试一下 现在,每当您想要使用新帐户和相关的回购时,请为相应的回购键入此选项

git remote add origin git@github-new-account-name:your-domain.com/your-repo.git

有很多答案解释如何完成你的要求,但我想提供一个不同的角度。

我认为把工作和个人事情分开是有道理的。在所有操作系统上切换用户帐户是非常简单的,所以你可以为工作和娱乐创建单独的操作系统帐户,并在每个操作系统中登录不同的github帐户。

保持清晰的关注点分离也可以……

防止你的工作文件和个人文件混在一起 降低对错误资源执行操作的风险 可能会将安全漏洞限制在一组帐户/凭据上

除了为多个帐户创建多个SSH密钥,您还可以考虑使用相同的帐户电子邮件在每个项目上添加合作者,并永久存储密码。

#this store the password permanently
$ git config --global credential.helper wincred

我用不同的电子邮件设置了多个帐户,然后把相同的用户和电子邮件放在每个帐户作为合作者之一。通过这种方式,我可以访问所有的帐户,无需添加SSH密钥,或切换到另一个用户名,和电子邮件进行身份验证。

这个答案是给初学者的(非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账户应该与这个回购链接,它只被问到一次,本地配置将记住这个链接,而不是全局配置,所以你可以在不同的回购上工作,链接到不同的账户,而不必每次编辑全局配置。