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

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

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

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

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


当前回答

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

其他回答

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

在我的情况下,我有我的工作帐户在Git-Lab和我的个人帐户在GitHub。 我的Git-Lab帐户被全局配置为可以从我笔记本电脑的所有目录访问,如下所示:

git config --global user.name WORK_USERNAME
git config --global user.email WORK_USERNAME@example.com

因此,如果你正在寻找一个不使用SSL的解决方案,你可以在一个空文件夹中使用git init,然后在该文件夹中插入你的个人帐户凭据:

git config user.name PERSONAL_USERNAME
git config user.email PERSONAL_USERNAME@example.com

注意这里,global没有设置,因为你只想从那里访问你的个人git,而不是在任何地方,所以文件夹内的任何东西都将其git凭据连接到你的个人帐户,而文件夹外的将连接到你的工作帐户。

之后,你可以克隆你的存储库,比如git克隆your_repo_link.git。然后会弹出一个新窗口,要求你登录你的github帐户。

要验证您的过程,请在您创建的文件夹中尝试git config——list,您应该会看到工作和个人用户名以及电子邮件及其目录。

在这个文件夹之外,如果你运行git config——list,你应该只看到你的工作用户名和电子邮件。

就是这样,希望这对没有配置SSL的人有所帮助。

只要在你最喜欢的编辑器中添加这一行,你就完成了一生

git remote set-url origin https://user-token-of-particular-user@github.com/profile-name/repo-name

刚刚在Windows上发现了这一点,为每次回购使用凭证:

cd c:\User1\SomeRepo
git config --local credential.https://github.com.user1 user1
git config --local credential.useHttpPath true
git config --local credential.helper manager
git remote set-url origin https://USERNAME@github.com/USERNAME/PROJECTNAME.git

credential.https://github.com的格式。告诉凭据助手凭据的URL。 “useHttpPath”告诉凭据管理器使用凭据的路径。如果省略了useHttpPath,则凭据管理器将为https://github.com存储一个凭据。如果包含了它,那么凭据管理器将存储多个凭据,这正是我真正想要的。

以防你不想弄乱~/。这里提到的Ssh /config文件,你可以运行git config core。ssh -i ~/.使用实例Ssh /custom_id_rsa”,你想从一个不同的帐户提交。

其余的设置是相同的:

使用SSH -keygen -t rsa -f ~/为第二个帐户创建新的SSH密钥。SSH -f ~/.ssh/custom_id_rsa 使用您的其他帐户登录github,转到https://github.com/settings/keys,并粘贴~/.ssh/custom_id_rsa.pub的内容 确保您使用SSH而不是HTTPS作为远程url: git remote set-url origin git@github.com:upstream_project_teamname/upstream_project.git