试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。
工作账户是先建立的,一切都完美无缺。
然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。
我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。
我如何用各自的GitHub凭证推/拉两个帐户?
试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。
工作账户是先建立的,一切都完美无缺。
然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。
我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。
我如何用各自的GitHub凭证推/拉两个帐户?
当前回答
只要在你最喜欢的编辑器中添加这一行,你就完成了一生
git remote set-url origin https://user-token-of-particular-user@github.com/profile-name/repo-name
其他回答
可以有多种方法来做到这一点,但以下解决方案为我工作,非常简单。 我不是试图用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帐户。 您可以将您的更改从主分支合并到主分支。
我在这里创建了主分支,根据你的选择命名你的分支。 还要附加屏幕截图。 在这里输入图像描述
如果你碰巧安装了WSL,你可以有两个独立的git帐户——一个在WSL上,一个在windows上。
您所需要做的就是使用多个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在评论中告诉我这件事。
如果您已经创建或克隆了另一个存储库,并且无法从原点或上游提取,则使用以下命令在该目录中添加ssh密钥是有效的。
这是我得到的错误:
Warning: Permanently added the RSA host key for IP address '61:fd9b::8c52:7203' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
我使用了下面的命令,这是有效的:
ssh-add ~ / . ssh / id_rsa_YOUR_COMPANY_NAME
塑形
要在单独的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==