试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。
工作账户是先建立的,一切都完美无缺。
然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。
我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。
我如何用各自的GitHub凭证推/拉两个帐户?
试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。
工作账户是先建立的,一切都完美无缺。
然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。
我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。
我如何用各自的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/bitbucket/任何帐户下管理git回购,你只需要生成一个新的SSH密钥。
但在我们开始用你的第二个身份推/拉回购之前,我们必须让你进入状态-让我们假设你的系统是用典型的id_rsa和id_rsa设置的。Pub密钥对。现在你的树~/。SSH是这样的
$ tree ~/.ssh
/Users/you/.ssh
├── known_hosts
├── id_rsa
└── id_rsa.pub
首先,命名该密钥对——添加一个描述性的名称将帮助您记住哪个密钥用于哪个用户/远程
# change to your ~/.ssh directory
$ cd ~/.ssh
# rename the private key
$ mv id_rsa github-mainuser
# rename the public key
$ mv id_rsa.pub github-mainuser.pub
接下来,让我们生成一个新的密钥对——这里我将新密钥命名为github-otheruser
$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/github-otheruser
现在,当我们看树~/。SSH我们看到了
$ tree ~/.ssh
/Users/you/.ssh
├── known_hosts
├── github-mainuser
├── github-mainuser.pub
├── github-otheruser
└── github-otheruser.pub
接下来,我们需要设置一个~/。Ssh /config文件将定义我们的关键配置。我们将使用适当的所有者只读/只写权限创建它
$ (umask 077; touch ~/.ssh/config)
用你最喜欢的编辑器打开它,并添加以下内容
Host github.com
User git
IdentityFile ~/.ssh/github-mainuser
Host github.com-otheruser
HostName github.com
User git
IdentityFile ~/.ssh/github-otheruser
假设,你会有一些现有的回购与你的主要github身份相关联。因此,“默认”github.com主机设置为使用mainuser密钥。如果您不想偏爱某个帐户而不是另一个帐户,我将向您展示如何更新系统上现有的回购以使用更新后的ssh配置。
将新的SSH密钥添加到github
转到github.com/settings/keys,添加新的公钥
你可以使用:复制/粘贴到github来获取公钥内容
$ cat ~/.ssh/github-otheruser.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDBVvWNQ2nO5...
现在您的新用户身份已经全部设置完毕-下面我们将向您展示如何使用它。
完成工作:克隆回购
那么这是如何一起工作与git和github?因为没有蛋就没有鸡,所以我们将研究克隆现有的回购。如果你的工作场所有一个新的github帐户,并且你被添加到一个公司项目中,这种情况可能适用于你。
假设github.com/someorg/somerepo已经存在,你被添加到它-克隆是一样容易
$ git clone github.com-otheruser:someorg/somerepo.git
粗体部分必须与我们在~/中设置的主机名匹配。ssh / config文件。正确地将git连接到相应的IdentityFile,并使用github正确地验证您的身份
完成工作:创建一个新的回购
好吧,因为你不能没有鸡和蛋,我们会考虑在你的二级账户上发布一个新的回购。这种情况适用于使用其次要github帐户创建新内容的用户。
让我们假设你已经在本地做了一些工作,现在准备推送到github。如果你愿意,你可以跟我一起走
$ cd ~
$ mkdir somerepo
$ cd somerepo
$ git init
现在配置这个回购使用您的身份
$ git config user.name "Mister Manager"
$ git config user.email "someuser@some.org"
现在做出你的第一个承诺
$ echo "hello world" > readme
$ git add .
$ git commit -m "first commit"
检查提交,看看你的新身份是使用git日志
$ git log --pretty="%H %an <%ae>"
f397a7cfbf55d44ffdf87aa24974f0a5001e1921 Mister Manager <someuser@some.org>
好了,该推到github了!因为github还不知道我们的新回购,首先去github.com/new并创建您的新回购-命名为somerepo
现在,为了配置你的repo使用正确的身份/凭据与github“对话”,我们已经添加了一个遥控器。假设你的github用户名为你的新帐户是someuser…
$ git remote add origin github.com-otheruser:someuser/somerepo.git
粗体部分非常重要,它必须与我们在~/中定义的Host相匹配。ssh / config文件
最后,推动回购
$ git push origin master
更新现有的repo以使用新的SSH配置
假设您已经克隆了一些回购,但现在想使用新的SSH配置。在上面的例子中,我们通过分配你之前的id_rsa/id_rsa来保持你现有的回购。pub密钥对到主机github.com在您的SSH配置文件。这没有什么问题,但我现在至少有5个github配置,我不喜欢把它们中的一个视为“默认”配置-我宁愿明确地说明每一个。
在这之前
Host github.com
User git
IdentityFile ~/.ssh/github-mainuser
Host github.com-otheruser
HostName github.com
User git
IdentityFile ~/.ssh/github-otheruser
现在我们将它更新为this(粗体部分的改动)
Host github.com-mainuser
HostName github.com
User git
IdentityFile ~/.ssh/github-mainuser
Host github.com-otheruser
HostName github.com
User git
IdentityFile ~/.ssh/github-otheruser
但现在任何现有的回购与github.com远程将无法与此身份文件工作。但别担心,这是一个简单的解决办法。
要更新任何现有的repo以使用新的SSH配置,请使用set-url -更新repo的远程原点字段
$ cd existingrepo
$ git remote set-url origin github.com-mainuser:someuser/existingrepo.git
就是这样。现在你可以尽情地推/拉了
SSH密钥文件权限
如果您遇到了公钥不能正常工作的问题,SSH对~/上允许的文件权限非常严格。SSH目录和相应的密钥文件
根据经验,任何目录都应该是700,任何文件都应该是600——这意味着它们是所有者只读/只读的——没有其他组/用户可以读/写它们
$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/config
$ chmod 600 ~/.ssh/github-mainuser
$ chmod 600 ~/.ssh/github-mainuser.pub
$ chmod 600 ~/.ssh/github-otheruser
$ chmod 600 ~/.ssh/github-otheruser.pub
如何管理SSH密钥
我为我连接到的每个主机管理单独的SSH密钥,这样,如果任何一个密钥被泄露,我不必在我使用该密钥的每个其他地方更新密钥。这就像你收到Adobe的通知,说他们有1.5亿用户的信息被盗了——现在你必须取消那张信用卡,并更新所有依赖于它的服务——多么讨厌。
这是我的~/。ssh目录如下:每个用户都有一个.pem密钥,在连接到的每个域的文件夹中。我使用.pem键,所以每个键只需要一个文件。
$ tree ~/.ssh
/Users/myuser/.ssh
├── another.site
│ ├── myuser.pem
├── config
├── github.com
│ ├── myuser.pem
│ ├── someusername.pem
├── known_hosts
├── somedomain.com
│ ├── someuser.pem
└── someotherdomain.org
└── root.pem
这是我对应的/。Ssh /配置文件——显然,github的东西与回答关于github的这个问题有关,但这个答案旨在让你具备在任何数量的服务/机器上管理Ssh身份的知识。
Host another.site
User muyuser
IdentityFile ~/.ssh/another.site/muyuser.pem
Host github.com-myuser
HostName github.com
User git
IdentityFile ~/.ssh/github.com/myuser.pem
Host github.com-someuser
HostName github.com
User git
IdentityFile ~/.ssh/github.com/someusername.pem
Host somedomain.com
HostName 162.10.20.30
User someuser
IdentityFile ~/.ssh/somedomain.com/someuser.pem
Host someotherdomain.org
User someuser
IdentityFile ~/.ssh/someotherdomain.org/root.pem
从PEM密钥中获取SSH公钥
上面您注意到,每个键只有一个文件。当我需要提供公钥时,我只需根据需要生成它。
因此,当github要求您的ssh公钥时,运行此命令将公钥输出到标准输出-在需要的地方复制/粘贴
$ ssh-keygen -y -f someuser.pem
ssh-rsa AAAAB3NzaC1yc2EAAAA...
注意,这也是我用于向任何远程机器添加密钥的相同过程。ssh-rsa AAAA…值复制到远程的~/。ssh / authorized_keys文件
转换您的id_rsa/id_rsa。pub密钥对为PEM格式
所以你想驯服你的密钥文件,减少一些文件系统的麻烦?将密钥对转换为单个PEM非常简单
$ cd ~/.ssh
$ openssl rsa -in id_rsa -outform pem > id_rsa.pem
或者,按照上面的例子,我们重命名id_rsa -> github-mainuser和id_rsa。Pub -> github-mainuser。酒吧- so
$ cd ~/.ssh
$ openssl rsa -in github-mainuser -outform pem > github-mainuser.pem
现在,为了确保转换正确,您需要验证生成的公钥是否与旧的公钥匹配
# display the public key
$ cat github-mainuser.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAA ... R++Nu+wDj7tCQ==
# generate public key from your new PEM
$ ssh-keygen -y -f someuser.pem
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAA ... R++Nu+wDj7tCQ==
现在你有了你的github-mainuser。Pem文件,您可以安全地删除旧的github-mainuser和github-mainuser。pub文件-只需要PEM文件;只要在你需要的时候生成公钥^_^
从头开始创建PEM密钥
您不需要创建私钥/公钥对,然后再转换为单个PEM密钥。可以直接创建PEM密钥。
让我们创建一个newuser.pem
$ openssl genrsa -out ~/.ssh/newuser.pem 4096
SSH公钥的获取方法相同
$ ssh-keygen -y -f ~/.ssh/newuser.pem
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACA ... FUNZvoKPRQ==
在一台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
另一种更简单的方法是使用多个桌面应用程序,就像我在做什么,使用帐户A在Github桌面,而使用帐户B在Github Kraken
可以有多种方法来做到这一点,但以下解决方案为我工作,非常简单。 我不是试图用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帐户。 您可以将您的更改从主分支合并到主分支。
我在这里创建了主分支,根据你的选择命名你的分支。 还要附加屏幕截图。 在这里输入图像描述
我看到这里有很多可行的变通办法。作为权宜之计,@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