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

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

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

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

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


当前回答

选项0:你不想乱搞操作系统设置..你只是想为一次回购提交一个不同的github账户,使用不同的公钥。

解决方案:

create the new key: ssh-keygen -t rsa -b 4096 -f ~/.ssh/alt_rsa add the key to the keyset: ssh-add -K ~/.ssh/alt_rsa copy and add the pub key to the github account: (see github instructions) test the key with github: ssh -i ~/.ssh/alt_rsa T git@github.com clone the repo using the git protocol (not HTTP): git clone git@github:myaccount... in the cloned repo: git config core.sshCommand "ssh -i ~/.ssh/alt_rsa -F /dev/null" git config user.name [myaccount] git config user.email [myaccount email] now you should be able to git push correctly without interferring with your everyday git account

其他回答

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

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

Go to ~/.ssh Create a file named config(have no extension ) Open config file & add below codes. (change according to your account) Account 1 # account_1 Host gitlab.com-account_1 HostName gitlab.com User git PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_account_1 Account 2 # Account2 Host gitlab.com-Account2 HostName gitlab.com User git PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_Account2 Account 3 # Account_3 Host github.com-Account3 HostName github.com User git PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_Account_3 Add remote url as follows Account 1 git remote add origin git@gitlab.com-account_1:group_name/repo_name.git Account 2 git remote add origin git@gitlab.com-Account2:group_name/repo_name.git Account 3 git remote add origin github.com-Account3:github_username/repo_name.git

确保IdentityFile名称与您在ssh密钥生成过程中创建的名称相同。

选项0:你不想乱搞操作系统设置..你只是想为一次回购提交一个不同的github账户,使用不同的公钥。

解决方案:

create the new key: ssh-keygen -t rsa -b 4096 -f ~/.ssh/alt_rsa add the key to the keyset: ssh-add -K ~/.ssh/alt_rsa copy and add the pub key to the github account: (see github instructions) test the key with github: ssh -i ~/.ssh/alt_rsa T git@github.com clone the repo using the git protocol (not HTTP): git clone git@github:myaccount... in the cloned repo: git config core.sshCommand "ssh -i ~/.ssh/alt_rsa -F /dev/null" git config user.name [myaccount] git config user.email [myaccount email] now you should be able to git push correctly without interferring with your everyday git account

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