试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。
工作账户是先建立的,一切都完美无缺。
然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。
我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。
我如何用各自的GitHub凭证推/拉两个帐户?
试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。
工作账户是先建立的,一切都完美无缺。
然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。
我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。
我如何用各自的GitHub凭证推/拉两个帐户?
当前回答
这个答案是给初学者的(非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账户应该与这个回购链接,它只被问到一次,本地配置将记住这个链接,而不是全局配置,所以你可以在不同的回购上工作,链接到不同的账户,而不必每次编辑全局配置。
其他回答
除了为多个帐户创建多个SSH密钥,您还可以考虑使用相同的帐户电子邮件在每个项目上添加合作者,并永久存储密码。
#this store the password permanently
$ git config --global credential.helper wincred
我用不同的电子邮件设置了多个帐户,然后把相同的用户和电子邮件放在每个帐户作为合作者之一。通过这种方式,我可以访问所有的帐户,无需添加SSH密钥,或切换到另一个用户名,和电子邮件进行身份验证。
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密钥生成过程中创建的名称相同。
您应该也不应该使用一些常见凭证推送到项目。在新机器上启动后,按照以下步骤正确设置和使用gitlab凭证:
在计算机上创建公共/私有SSH密钥 复制粘贴公钥到gitlab/github UI界面(任何通过CMD行提示如何操作的人都可以获得免费啤酒…) 请确保您通过git而不是HTTP url克隆回购 设置git别名,以避免git命令经常输入相同的前缀 在git提交期间,总是使用author和e-mail标记 使用git正常你会这样做
这一切如下:
# create the public / private key credentials on that specific machine
ssh-keygen -t rsa -b 4096 -C "<<you>>@org.net" -f ~/.ssh/id_rsa.<<you>>.`hostname -s`
# setup your public key in the gitlab ui
cat ~/.ssh/id_rsa.<<you>>.`hostname -s`
# make sure you clone the repo via the git and not http url
git clone git@git.in.org.net:org/some-repo.git
# set the git alias to avoid constant typing of the repeating prefix to the git cmd
alias git='GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa.<<you>>.`hostname -s`" git'
# during git commit ALWAYS use the author and e-mail flags
git add --all ; git commit -nm "$git_msg" --author "YourFirstName YourLastName <you@phz.fi>"
# use git as normal
git fetch --all; git pull --all
相反,您只需将新的电子邮件id添加到您的个人git帐户。这样您就不需要添加另一个SSH密钥。只需按以下步骤配置新邮箱即可 Git配置——全局用户。电子邮件newemail。然后你就可以复制那个回购了。
使用个人访问令牌的个人目录.gitconfig
如果您不想修改您的主机文件,使用SSH密钥,或者为每个repo设置一个.gitconfig,那么您可以使用一个个人的.gitconfig,它基本上包含在根级配置中。给定一个OSX目录结构
# root level git config
~/.gitconfig
# your personal repos under some folder like
../personal/
../home/
~/Dropbox/
在你的个人文件夹中添加一个.gitconfig,比如~/Dropbox/.gitconfig
[user]
email = first.last@home.com
name = First Last
[credential]
username = PersonalGithubUsername
helper = osxkeychain
在根级.gitconfig中添加includeIf部分,以便在您的个人目录中获取您的个人配置。这里的任何设置都将覆盖根配置,只要includeIf出现在您想要覆盖的设置之后。
[user]
email = first.last@work.com
name = "First Last"
[credential]
helper = osxkeychain
[includeIf "gitdir:~/Dropbox/**"]
path = ~/Dropbox/.gitconfig
尝试推动你的个人回购或从你的私人回购
git push
# prompts for password
当出现提示时,输入您的个人密码,或者最好输入您在帐户开发人员设置中创建的个人访问令牌。输入该令牌作为密码。
假设你已经在使用git-credential-osxkeychain,你的个人凭证应该存储在你的钥匙链中,所以两个github条目将显示,但使用不同的帐户。