试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。

工作账户是先建立的,一切都完美无缺。

然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。

我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。

我如何用各自的GitHub凭证推/拉两个帐户?


当前回答

除了为多个帐户创建多个SSH密钥,您还可以考虑使用相同的帐户电子邮件在每个项目上添加合作者,并永久存储密码。

#this store the password permanently
$ git config --global credential.helper wincred

我用不同的电子邮件设置了多个帐户,然后把相同的用户和电子邮件放在每个帐户作为合作者之一。通过这种方式,我可以访问所有的帐户,无需添加SSH密钥,或切换到另一个用户名,和电子邮件进行身份验证。

其他回答

让我的私人回购工作使用SSH密钥对。这是在Windows git上测试的。

来源:https://docs.github.com/en/free-pro-team@latest github / authenticating-to-github / generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

A.生成公钥和私钥对

启动git bash 执行ssh-keygen -t ed25519 -C "your_email@example.com" 当提示您“输入要保存密钥的文件”时,按Enter接受默认值。 按enter键输入空白密码。 启动ssh代理:eval $(ssh-agent) 将私钥添加到ssh代理,并存储密码:ssh- Add ~/.ssh/id_ed25519

B.添加SSH密钥到GitHub账户

将公钥复制到剪贴板:clip < ~/.ssh/id_ed25519.pub 在GitHub中,进入“配置文件->设置-> SSH密钥-> SSH新密钥” 给出一个标题。如。“MacBook Pro上的Windows” 粘贴密钥并点击“添加SSH密钥”。

C.测试SSH连接

输入:ssh -T git@github.com 点击“是”查看任何警告信息。 它应该显示:“Hi username!…”,表示测试成功。

D.设置本地存储库以使用SSH密钥

更改邮箱和用户名:

git config user.email your_email@example.com
git config user.name github_username

更新远程链接以使用git。首先列出远程URI:

git remote -v
git remote set-url origin git@github.com:github_username/your-repo-name.git

e .测试

git remote show origin

塑形

要在单独的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==

您所需要做的就是使用多个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在评论中告诉我这件事。

我使用shell脚本将我切换到任何我想要“活跃”的帐户。从本质上讲,您可以从头开始,正确配置一个帐户并正常工作,然后将这些文件移动到具有适当前缀的名称。从那时起,你可以使用命令“github”,或“gitxyz”切换:

# my github script
cd ~/.ssh

if [ -f git_dhoerl -a -f git_dhoerl.pub -a -f config_dhoerl ]
then
    ; 
else 
    echo "Error: missing new files"
    exit 1
fi 

# Save a copy in /tmp, just in case
cp id_rsa /tmp
cp id_rsa.pub /tmp
cp config /tmp
echo "Saved old files in /tmp, just in case"

rm id_rsa
rm id_rsa.pub
rm config
echo "Removed current links/files"

ln git_dhoerl id_rsa
ln git_dhoerl.pub id_rsa.pub
ln config_dhoerl config

git config --global user.email "dhoerl@<company>.com"
git config --global github.user "dhoerl"        
git config --global github.token "whatever_it_is"

ssh-add -D

我在这方面运气不错。我还在Xcode中创建了一个运行脚本(为Mac用户),所以它不会构建我的项目,除非我有适当的设置(因为它使用git):

运行脚本放在Dependencies之后(使用/bin/ksh作为shell):

if [ "$(git config --global --get user.email)" != "dhoerl@<company>.com" ]
then
    exit 1
fi

编辑:添加测试新文件是否存在,并将旧文件复制到/tmp以解决下面@naomik的注释。

这个答案是给初学者的(非git大师)。我最近遇到了这个问题,也许这只是我的问题,但大多数答案似乎需要对git有相当深入的了解。在阅读了包括这个线程在内的几个堆栈溢出的答案后,下面是我需要采取的步骤,以便在GitHub帐户之间轻松切换(例如,假设有两个GitHub帐户,github.com/personal和gitHub.com/work):

Check for existing ssh keys: Open Terminal and run this command to see/list existing ssh keys ls -al ~/.ssh files with extension .pub are your ssh keys so you should have two for the personal and work accounts. If there is only one or none, its time to generate other wise skip this.- Generating ssh key: login to github (either the personal or work acc.), navigate to Settings and copy the associated email.now go back to Terminal and run ssh-keygen -t rsa -C "the copied email", you'll see:Generating public/private rsa key pair. Enter file in which to save the key (/.../.ssh/id_rsa): id_rsa is the default name for the soon to be generated ssh key so copy the path and rename the default, e.g. /.../.ssh/id_rsa_work if generating for work account. provide a password or just enter to ignore and, you'll read something like The key's randomart image is: and the image. done.Repeat this step once more for your second github account. Make sure you use the right email address and a different ssh key name (e.g. id_rsa_personal) to avoid overwriting. At this stage, you should see two ssh keys when running ls -al ~/.ssh again. Associate ssh key with gitHub account: Next step is to copy one of the ssh keys, run this but replacing your own ssh key name: pbcopy < ~/.ssh/id_rsa_work.pub, replace id_rsa_work.pub with what you called yours.Now that our ssh key is copied to clipboard, go back to github account [Make sure you're logged in to work account if the ssh key you copied is id_rsa_work] and navigate toSettings - SSH and GPG Keys and click on New SSH key button (not New GPG key btw :D) give some title for this key, paste the key and click on Add SSH key. You've now either successfully added the ssh key or noticed it has been there all along which is fine (or you got an error because you selected New GPG key instead of New SSH key :D). Associate ssh key with gitHub account: Repeat the above step for your second account. Edit the global git configuration: Last step is to make sure the global configuration file is aware of all github accounts (so to say). Run git config --global --edit to edit this global file, if this opens vim and you don't know how to use it, press i to enter Insert mode, edit the file as below, and press esc followed by :wq to exit insert mode: [inside this square brackets give a name to the followed acc.] name = github_username email = github_emailaddress [any other name] name = github_username email = github_email [credential] helper = osxkeychain useHttpPath = true

完成了!现在,当尝试从一个回购中推或拉,你会被问到哪个GitHub账户应该与这个回购链接,它只被问到一次,本地配置将记住这个链接,而不是全局配置,所以你可以在不同的回购上工作,链接到不同的账户,而不必每次编辑全局配置。