试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。
工作账户是先建立的,一切都完美无缺。
然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。
我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。
我如何用各自的GitHub凭证推/拉两个帐户?
试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。
工作账户是先建立的,一切都完美无缺。
然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。
我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。
我如何用各自的GitHub凭证推/拉两个帐户?
当前回答
使用HTTPS:
将远程url更改为https:
git remote set-url origin https://USERNAME@github.com/USERNAME/PROJECTNAME.git
你可以开始了:
git push
为了确保提交显示为USERNAME执行,可以设置user.name和user。为这个项目发送电子邮件:
git config user.name USERNAME
git config user.email USERNAME@example.com
其他回答
选项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
使用码头工人!
这可能不适合每个人,但这是我最后为自己做的。
我所有的开发现在都在docker容器中完成。我是一个vim用户,但我知道VSCode有插件来使用docker容器。
因此,由于我有不同的docker容器为不同的项目/语言,我可以在每个内部使用不同的git配置。问题解决了。
在一台Windows机器上管理多个GitHub帐户(HTTPS)
假设你之前在你的机器上使用git并配置了git全局配置文件。要检查它,打开终端,然后:
git config --global -e
它打开你的编辑器,你可能会看到这样的东西:
[user]
email = yourEmail@gmail.com
name = Your_Name
...
这很好,因为你可以把你的代码推送到GitHub账户,而不用每次都输入凭证。 但如果它需要从另一个账户推送回购呢?在这种情况下,git将拒绝403 err,您必须更改全局git凭据。要在凭据管理器中存储一个回购名称,可以使用这个lat集:
git config --global credential.github.com.useHttpPath true
要检查它打开配置一次 Git配置——global -e 您将看到新的配置行
[credential]
useHttpPath = true
...
就是它。现在,当你第一次推到任何帐户,你会看到一个弹出 Screenshot_1
输入特定的此回购帐户凭据,这将“绑定”此帐户的回购。因此,在您的机器中,您可以指定任意数量的帐户/回购。
想要更详细的解释,你可以看看我在youtube上找到的这个很酷的视频: https://youtu.be/2MGGJtTH0bQ
您应该也不应该使用一些常见凭证推送到项目。在新机器上启动后,按照以下步骤正确设置和使用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
最简单和直接的方法(IMHO) -没有配置文件,没有太多的麻烦
只需创建另一个ssh密钥。
假设你有一个新的GitHub工作帐户,只需为它创建一个新密钥:
ssh-keygen -t rsa -C "email@work_mail.com" -f "id_rsa_work_user1"`
您只需要运行一次上面的程序。
现在你应该有了旧的和新的,要查看它们,运行:
ls -al ~/.ssh
从现在开始,每次你想在两者之间切换时,只需运行:
ssh-add -D
ssh-add ~/.ssh/id_rsa_work_user1 #make to use this without the suffix .pub
为了切换到旧的,再次运行:
ssh-add -D
ssh-add ~/.ssh/<previous id_rsa>