试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。
工作账户是先建立的,一切都完美无缺。
然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。
我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。
我如何用各自的GitHub凭证推/拉两个帐户?
试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。
工作账户是先建立的,一切都完美无缺。
然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。
我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。
我如何用各自的GitHub凭证推/拉两个帐户?
当前回答
导航到要将更改推到另一个GitHub帐户的目录。 在终端/命令行中创建一个新的SSH密钥。 ssh-keygen -t rsa -C " your-email-address " 下面将显示: 生成rsa公私钥对。 输入保存密钥的文件(/home/your_username/.ssh/id_rsa):
复制并粘贴路径,后面跟着一个可识别的文件名:
/home/your_username/.ssh/id_rsa_personal
4)然后它会问你以下问题:
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
5)您现在可以输入以下命令查看您在本地机器上拥有的所有SSH密钥:
ls -al ~/.ssh
您应该能够看到新的SSH密钥文件。正如你可以看到在我的一个,我有id_rsa_test和id_rsa_personal.pub。
drwx------ 2 gmadmin gmadmin 4096 Nov 16 22:20 .
drwxr-xr-x 42 gmadmin gmadmin 4096 Nov 16 21:03 ..
-rw------- 1 gmadmin gmadmin 1766 Nov 16 22:20 id_rsa_personal
-rw-r--r-- 1 gmadmin gmadmin 414 Nov 16 22:20 id_rsa_personal.pub
-rw-r--r-- 1 gmadmin gmadmin 444 Nov 6 11:32 known_hosts
6)接下来需要复制存储在id_rsa_personal中的SSH密钥。酒吧文件。您可以在您选择的文本编辑器中打开此文件。我目前正在使用atom,所以我使用以下命令打开文件:
atom ~/.ssh/id_rsa_personal.pub
然后你会得到类似这样的结果:
ssh-rsa AAB3HKJLKC1yc2EAAAADAQABAAAAQCgU5+ELtwsKkmcoeF3hNd7d6CjW+dWut83R/DC01E/YzLc5ZFri18doOwuQoeTPpmIRVDGuQQsZshjDrTkFy8rwKWMlXl7va5olnGICcpg4qydEtsW+MELDmayW1HHsi2xHMMGHlNv
7)复制并导航到您的GitHub帐户→设置→SSH和GPG密钥 8)单击“新建SSH密钥”。复制键,给它一个标题并添加它。 9)从终端添加密钥
ssh-add ~/.ssh/id_rsa_personal
Enter passphrase for /home/your_username/.ssh/id_rsa_personal:
10)配置用户和密码。
git config --global user.name "gitusername"
git config --global user.email "gitemail"
11)我们已经做好了承诺和推进的准备。
git init
git add .
git commit
git push
其他回答
您所需要做的就是使用多个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在评论中告诉我这件事。
我看到这里有很多可行的变通办法。作为权宜之计,@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
我使用shell脚本将我切换到任何我想要“活跃”的帐户。从本质上讲,您可以从头开始,正确配置一个帐户并正常工作,然后将这些文件移动到具有适当前缀的名称。从那时起,你可以使用命令“github”,或“gitxyz”切换:
# my github script
cd ~/.ssh
if [ -f git_dhoerl -a -f git_dhoerl.pub -a -f config_dhoerl ]
then
;
else
echo "Error: missing new files"
exit 1
fi
# Save a copy in /tmp, just in case
cp id_rsa /tmp
cp id_rsa.pub /tmp
cp config /tmp
echo "Saved old files in /tmp, just in case"
rm id_rsa
rm id_rsa.pub
rm config
echo "Removed current links/files"
ln git_dhoerl id_rsa
ln git_dhoerl.pub id_rsa.pub
ln config_dhoerl config
git config --global user.email "dhoerl@<company>.com"
git config --global github.user "dhoerl"
git config --global github.token "whatever_it_is"
ssh-add -D
我在这方面运气不错。我还在Xcode中创建了一个运行脚本(为Mac用户),所以它不会构建我的项目,除非我有适当的设置(因为它使用git):
运行脚本放在Dependencies之后(使用/bin/ksh作为shell):
if [ "$(git config --global --get user.email)" != "dhoerl@<company>.com" ]
then
exit 1
fi
编辑:添加测试新文件是否存在,并将旧文件复制到/tmp以解决下面@naomik的注释。
简单的方法,不需要处理SSH键盘
只需更改user.name & user。在回购的本地电子邮件。给那些你想要推到回购的人。
示例:我在gitlab有一个工作帐户,有一个项目。克隆这个项目/回购后,在终端中,我输入:
git config user.name "my-work-username"
git config user.email "my-work-email-id"
现在,假设我在Gitlab中有另一个个人项目/回购,我想将它与我的个人帐户关联起来。就像上面一样,克隆后,我输入:
git config user.name "my-personal-username"
git config user.email "my-personal-email-id"
希望这能有所帮助。如果对你有用就给它点赞!:)
使用个人访问令牌的个人目录.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条目将显示,但使用不同的帐户。