试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。
工作账户是先建立的,一切都完美无缺。
然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。
我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。
我如何用各自的GitHub凭证推/拉两个帐户?
试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。
工作账户是先建立的,一切都完美无缺。
然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。
我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。
我如何用各自的GitHub凭证推/拉两个帐户?
当前回答
可以有多种方法来做到这一点,但以下解决方案为我工作,非常简单。 我不是试图用SSH,我的步骤和解决方案是基于HTTPS。
Create your project DIR on your local machine. Example d:\test_git_multiple_account go to the folder "test_git_multiple_account" Add few files here into the DIR Open Git bash here and run following command a. git init // initialization b. git add , // add c. git commit -m "initial commit" you will get following output : in my case i use to add one python file created from code. **[master (root-commit) d4defd9] initial commit 2 files changed, 4 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 Hello.py** d. git remote add origin <HTTPS repo link> e. git remote -v // check the repo version f. git push origin master it will ask your git hub user name and password via popup screen. you will get the following output Counting objects: 100% (5/5), done. Delta compression using up to 4 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (5/5), 411 bytes | 31.00 KiB/s, done. Total 5 (delta 0), reused 0 (delta 0), pack-reused 0 remote: remote: Create a pull request for 'master' on GitHub by visiting: remote: https://github.com/vishvadeepaktripathi/Hello_Py/pull/new/master remote: To https://github.com/vishvadeepaktripathi/Hello_Py.git * [new branch] master -> master
这将创建一个名为master的新分支。 你可以提交到主分支一旦你改变了分支,在这种情况下,你现有的文件将被删除。所以我建议签入主分支到第一步,然后继续为每个命令,如果你想直接签入主分支。 在第一次登录时,它可能会给你一个错误消息,并再次要求登录名和密码,然后它会将您的更改发布到Git中心。
一旦这样做,你会得到消息到新的拉请求到你的github帐户。 您可以将您的更改从主分支合并到主分支。
我在这里创建了主分支,根据你的选择命名你的分支。 还要附加屏幕截图。 在这里输入图像描述
其他回答
您应该也不应该使用一些常见凭证推送到项目。在新机器上启动后,按照以下步骤正确设置和使用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
将github中的认证方式由SSh修改为HTTP。这样它就不会关心你是否登录了你的工作github帐户,你试图远程与你的个人github帐户(它有不同的凭据,而不是你目前登录到你的本地机器)交互。
不像其他答案,在那里你需要遵循几个步骤来使用两个不同的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 /配置文件,如在其他答案中提到的,如果您的两个帐户的主机名不同。
只要在你最喜欢的编辑器中添加这一行,你就完成了一生
git remote set-url origin https://user-token-of-particular-user@github.com/profile-name/repo-name
使用码头工人!
这可能不适合每个人,但这是我最后为自己做的。
我所有的开发现在都在docker容器中完成。我是一个vim用户,但我知道VSCode有插件来使用docker容器。
因此,由于我有不同的docker容器为不同的项目/语言,我可以在每个内部使用不同的git配置。问题解决了。