试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。
工作账户是先建立的,一切都完美无缺。
然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。
我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。
我如何用各自的GitHub凭证推/拉两个帐户?
试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。
工作账户是先建立的,一切都完美无缺。
然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。
我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。
我如何用各自的GitHub凭证推/拉两个帐户?
当前回答
刚刚在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存储一个凭据。如果包含了它,那么凭据管理器将存储多个凭据,这正是我真正想要的。
其他回答
刚刚在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密钥,您还可以考虑使用相同的帐户电子邮件在每个项目上添加合作者,并永久存储密码。
#this store the password permanently
$ git config --global credential.helper wincred
我用不同的电子邮件设置了多个帐户,然后把相同的用户和电子邮件放在每个帐户作为合作者之一。通过这种方式,我可以访问所有的帐户,无需添加SSH密钥,或切换到另一个用户名,和电子邮件进行身份验证。
更简单和容易修复,以避免混乱..
Windows用户可以为不同的项目使用多个或不同的git帐户。
以下步骤: 进入控制面板,搜索凭据管理器。 然后转到凭证管理器-> Windows凭证
现在在Generic Credentials Heading下删除git:https//github.com节点
这将删除当前凭据。现在你可以通过git添加任何项目,它会要求用户名和密码。
当你遇到任何问题与其他帐户做同样的过程。
您所需要做的就是使用多个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在评论中告诉我这件事。
我使用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的注释。