试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。
工作账户是先建立的,一切都完美无缺。
然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。
我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。
我如何用各自的GitHub凭证推/拉两个帐户?
试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。
工作账户是先建立的,一切都完美无缺。
然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。
我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。
我如何用各自的GitHub凭证推/拉两个帐户?
当前回答
2023年更新:
你可以使用Github桌面。在这里,你只需要在本地克隆存储库,并使用Github Desktop管理代码。
其他回答
让我的私人回购工作使用SSH密钥对。这是在Windows git上测试的。
来源:https://docs.github.com/en/free-pro-team@latest github / authenticating-to-github / generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
A.生成公钥和私钥对
启动git bash 执行ssh-keygen -t ed25519 -C "your_email@example.com" 当提示您“输入要保存密钥的文件”时,按Enter接受默认值。 按enter键输入空白密码。 启动ssh代理:eval $(ssh-agent) 将私钥添加到ssh代理,并存储密码:ssh- Add ~/.ssh/id_ed25519
B.添加SSH密钥到GitHub账户
将公钥复制到剪贴板:clip < ~/.ssh/id_ed25519.pub 在GitHub中,进入“配置文件->设置-> SSH密钥-> SSH新密钥” 给出一个标题。如。“MacBook Pro上的Windows” 粘贴密钥并点击“添加SSH密钥”。
C.测试SSH连接
输入:ssh -T git@github.com 点击“是”查看任何警告信息。 它应该显示:“Hi username!…”,表示测试成功。
D.设置本地存储库以使用SSH密钥
更改邮箱和用户名:
git config user.email your_email@example.com
git config user.name github_username
更新远程链接以使用git。首先列出远程URI:
git remote -v
git remote set-url origin git@github.com:github_username/your-repo-name.git
e .测试
git remote show origin
不像其他答案,在那里你需要遵循几个步骤来使用两个不同的github帐户从同一台机器,对我来说,它在两个步骤。
你只需要:
1)在~/下为您的每个帐户生成SSH公私钥对。使用不同的名称和SSH位置
2)在“设置”中,将生成的公钥添加到对应的帐号“>> SSH和GPG密钥>> SSH新密钥”。
使用实例生成SSH公私钥对。
cd ~/.ssh
ssh-keygen -t rsa -C "email@work.com" -f "id_rsa_WORK"
ssh-keygen -t rsa -C "email@gmail.com" -f "id_rsa_PERSONAL"
作为上述命令的结果,id_rsa_WORK和id_rsa_WORK。pub文件将为您的工作帐户(ex - git.work.com)和id_rsa_PERSONAL和id_rsa_PERSONAL创建。Pub将为您的个人帐户(ex - github.com)创建。
创建之后,从每个公共(*.pub)文件复制内容,并对每个帐户执行步骤2。
PS:没有必要为~/下的每个git帐户创建一个主机条目。Ssh /配置文件,如在其他答案中提到的,如果您的两个帐户的主机名不同。
使用个人访问令牌的个人目录.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条目将显示,但使用不同的帐户。
您所需要做的就是使用多个SSH密钥对配置您的SSH设置。
这个链接很容易理解(谢谢Eric): http://code.tutsplus.com/tutorials/quick-tip-how-to-work-with-github-and-multiple-accounts--net-22574 生成SSH密钥(Win/msysgit): https://help.github.com/articles/generating-an-ssh-key/
第一个环节的相关步骤:
Generate an SSH-key: ssh-keygen -t ed25519 -C "john@doe.example.com" Follow the prompts and decide a name, e.g. id_ed25519_example_company. Copy the SSH public-key to GitHub from ~/.ssh/id_ed25519_doe_company.pub and tell ssh about the key: ssh-add ~/.ssh/id_ed25519_doe_company Create a config file in ~/.ssh with the following contents: Host github-doe-company HostName github.com User git IdentityFile ~/.ssh/id_ed25519_doe_company Add your remote: git remote add origin git@github-doe-company:username/repo.git or change using: git remote set-url origin git@github-doe-company:username/repo.git
此外,如果你正在使用多个使用不同角色的存储库,你需要确保你的各个存储库有相应的用户设置覆盖:
设置用户名,电子邮件和GitHub令牌-覆盖个人回购的设置 https://help.github.com/articles/setting-your-commit-email-address-in-git/
注意: 有些人可能需要不同的电子邮件用于不同的存储库,从git 2.13开始,你可以通过编辑全局配置文件在目录的基础上设置电子邮件:~/。Gitconfig使用这样的条件:
[user]
name = Default Name
email = defaultemail@example.com
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig
然后是特定于工作的配置~/work/。Gitconfig看起来是这样的:
[user]
name = Pavan Kataria
email = pavan.kataria@example.com
谢谢@alexg在评论中告诉我这件事。
如果您已经创建或克隆了另一个存储库,并且无法从原点或上游提取,则使用以下命令在该目录中添加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