git 多个bitbucket帐户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8186441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:20:18  来源:igfitidea点击:

Multiple bitbucket accounts

gitsshbitbucket

提问by TheWebGuy

I have a Bitbucket account for my 9-5 job and I also have a personal Bitbucket account. My goal is to be able to use both on the same computer. I have installed the latest git on a Windows 7 pc.

我有一个适用于朝九晚五工作的 Bitbucket 帐户,而且我还有一个个人 Bitbucket 帐户。我的目标是能够在同一台计算机上使用两者。我已经在 Windows 7 电脑上安装了最新的 git。

So currently everything with my companies Bitbucket account works fine, I can pull/push with no problems. I created a new ssh key using ssh-keygen and assigned a new name in my case "tech". But I am having issues on how to tell a local repo to use the new ssh key I created. I am assuming everytime I try to connect it uses the first ssh key.

因此,目前我公司的 Bitbucket 帐户的所有内容都可以正常工作,我可以毫无问题地拉/推。我使用 ssh-keygen 创建了一个新的 ssh 密钥,并在我的案例“tech”中分配了一个新名称。但是我在如何告诉本地存储库使用我创建的新 ssh 密钥方面遇到了问题。我假设每次尝试连接它时都使用第一个 ssh 密钥。

I get the error:

我收到错误:

$ git push conq: repository access denied. fatal: The remote end hung up unexpectedly

$ git push conq:存储库访问被拒绝。致命:远端意外挂断

I found some advice on the internet but it seems to relate to a linux/git setup, for example I can't find the "config" file on windows.

我在互联网上找到了一些建议,但它似乎与 linux/git 设置有关,例如我在 Windows 上找不到“配置”文件。

采纳答案by MoxieandMore

You may get this error if you haven't added the key to the key manager (ssh-agent). To do this:

如果您尚未将密钥添加到密钥管理器 (ssh-agent),您可能会收到此错误。去做这个:

ssh-add ~/.ssh/tech

By the way, if you have multiple Bitbucket accounts, you'll need a unique key for each account. You can't reuse keys.

顺便说一下,如果您有多个 Bitbucket 帐户,则每个帐户都需要一个唯一的密钥。您不能重复使用密钥。

回答by ZenBalance

This blog postdescribes a straightforward way to add multiple ssh keys to a single computer and use one ssh key per a bitbucket account. It is much clearer than the official bitbucket documentation. To summarize:

这篇博文描述了一种向一台计算机添加多个 ssh 密钥并为每个 bitbucket 帐户使用一个 ssh 密钥的简单方法。它比官方的 bitbucket 文档清楚得多。总结一下:

First, make sure you have a default account setup through a tutorial like this one on Github.

首先,确保您通过Github 上的此类教程设置了默认帐户。

For the second account:

对于第二个帐户:

  1. Create a new ssh key:

    ssh-keygen -f ~/.ssh/<your second account name> -C "<you email>"
    
  2. Use pbcopy < ~/.ssh/<your second account name>.pubto copy the public key and add this key to your bitbucket account (in the settings area)

  1. 创建一个新的 ssh 密钥:

    ssh-keygen -f ~/.ssh/<your second account name> -C "<you email>"
    
  2. 使用pbcopy < ~/.ssh/<your second account name>.pub复制的公钥和此键添加到您的帐户到位桶(在设置区域)

(On Windows you can copy the ssh key using ssh-keygen -f ~/.ssh/<your account name> -c "<your email>" | clipor on Linux you can follow these instructions.

(在 Windows 上,您可以使用ssh-keygen -f ~/.ssh/<your account name> -c "<your email>" | clip或在Linux上复制 ssh 密钥,您可以按照这些说明进行操作。

  1. Add the following to your ~/.ssh/configfile. The first sets the default key for bitbucket.org. The second sets your second key to an alias bitbucket-account2for bitbucket.org:

    Host bitbucket.org
      Hostname bitbucket.org
      IdentityFile ~/.ssh/id_rsa
    
    Host bitbucket-account2
      Hostname bitbucket.org
      PreferredAuthentications publickey
      IdentityFile ~/.ssh/<your second account name>
    
  2. You can now clone projects with your default account the same way as before:

    git clone [email protected]:username/project.git
    
  3. To clone a project with the second identity, replace bitbucket.org with the Host that you specified in the ~/.ssh/configfile (i.e. bitbucket-account2above):

    git clone git@bitbucket-account2:username/project.git
    
  1. 将以下内容添加到您的~/.ssh/config文件中。第一个设置 bitbucket.org 的默认密钥。第二个将您的第二个密钥设置bitbucket-account2为 bitbucket.org的别名:

    Host bitbucket.org
      Hostname bitbucket.org
      IdentityFile ~/.ssh/id_rsa
    
    Host bitbucket-account2
      Hostname bitbucket.org
      PreferredAuthentications publickey
      IdentityFile ~/.ssh/<your second account name>
    
  2. 您现在可以像以前一样使用默认帐户克隆项目:

    git clone [email protected]:username/project.git
    
  3. 要克隆具有第二个身份的项目,请将 bitbucket.org 替换为您在~/.ssh/config文件中指定的主机(即bitbucket-account2上面):

    git clone git@bitbucket-account2:username/project.git
    

That's it!

就是这样!

回答by Nic

As stated, you only need to generate your pubkey once - since you're already setup with BitBucket, where is your id_rsa(or whatever you named yours) file? On our Windows installs, it's under the user's home directory in the hidden folder .ssh. You should be able to create a configfile there.

如前所述,您只需要生成一次公钥 - 因为您已经使用 BitBucket 进行了设置,那么您的id_rsa(或您命名的)文件在哪里?在我们的 Windows 安装中,它位于隐藏文件夹中的用户主目录下.ssh。您应该能够在config那里创建一个文件。

回答by kan

You should generate public/private key pair only once. Then all hosts which have your public key do allow connections from you if you provide the private key.

您应该只生成一次公钥/私钥对。然后,如果您提供私钥,那么所有拥有您的公钥的主机都允许来自您的连接。

回答by Cody

You can add your company email in your personal bitbucket account, In bitbucket account manage page:

您可以在您的个人 bitbucket 帐户中添加您的公司电子邮件,在 bitbucket 帐户管理页面中:

enter image description here

在此处输入图片说明

You can login your personal email account, and access both personal projects and company projects in single bitbucket account, which is using only one ssh private key.

您可以登录您的个人电子邮件帐户,在一个bitbucket帐户中访问个人项目和公司项目,仅使用一个ssh私钥。