同一台电脑上有多个 GitHub 帐户吗?

发布于 2024-09-26 06:13:34 字数 202 浏览 2 评论 0 原文

尝试从我的计算机上处​​理我实际的“工作”存储库和 GitHub 上的存储库。

首先设置了工作帐户,一切正常。

但是,我的帐户似乎无法推送到我的存储库,该存储库是在不同的帐户/电子邮件下设置的。

我尝试将工作密钥复制到我的帐户,但这会引发错误,因为密钥只能附加到一个帐户。

如何使用各自的 GitHub 凭据向两个帐户推送/拉取它们?

Trying to work on both my actual "work" repos, and my repos on GitHub, from my computer.

The work account was set up first, and everything works flawlessly.

My account, however, cannot seem to push to my repo, which is set up under a different account/email.

I've tried copying my work key up to my account, but that throws an error because of course a key can only be attached to one account.

How can I push/pull to and from both accounts with their respective GitHub credentials?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

娇俏 2024-10-03 06:13:35

使用个人访问令牌的个人目录 .gitconfig

如果您不想修改主机文件、使用 SSH 密钥或为每个存储库设置 .gitconfig,那么您可以使用基本上从根级别配置中包含的个人.gitconfig。给定一个 OSX 目录结构,例如

# root level git config
~/.gitconfig

# your personal repos under some folder like
../personal/
../home/
~/Dropbox/

在您的个人文件夹中添加 .gitconfig,例如 ~/Dropbox/.gitconfig

[user]
    email = [email protected]
    name = First Last
[credential]
    username = PersonalGithubUsername
    helper = osxkeychain

在您的根级别 .gitconfig添加 includeIf 部分,以便在您位于个人目录时获取您的个人配置。只要 includeIf 位于您要覆盖的设置之后,任何设置都将覆盖根配置。

[user]
    email = [email protected]
    name = "First Last"
[credential]
    helper = osxkeychain
[includeIf "gitdir:~/Dropbox/**"]
    path = ~/Dropbox/.gitconfig

尝试推送到您的个人存储库或从您的私人存储库中提取

git push
# prompts for password

当提示时输入您的个人密码,或者更好的是,您在 帐户开发者设置。输入该令牌作为您的密码。

假设您已经在使用 git-credential -osxkeychain,您的个人凭据应存储在您的钥匙串中,因此将显示两个 github 条目,但具有不同的帐户。

OSX 钥匙串2 个完整的 Github

Personal Directory .gitconfig using a personal access token

If you do not want to modify your host file, use SSH keys, or setup a .gitconfig for each repo, then you may use a personal .gitconfig that you basically include from the root level config. Given an OSX directory structure like

# root level git config
~/.gitconfig

# your personal repos under some folder like
../personal/
../home/
~/Dropbox/

Add a .gitconfig in your personal folder, such as ~/Dropbox/.gitconfig

[user]
    email = [email protected]
    name = First Last
[credential]
    username = PersonalGithubUsername
    helper = osxkeychain

In your root level .gitconfig add an includeIf section to source your personal config whenever you are in your personal directory. Any settings there will override the root config as long as the includeIf comes after the settings you want to override.

[user]
    email = [email protected]
    name = "First Last"
[credential]
    helper = osxkeychain
[includeIf "gitdir:~/Dropbox/**"]
    path = ~/Dropbox/.gitconfig

Try pushing to your personal repo or pulling from your private repo

git push
# prompts for password

When prompted enter either your personal password or, better yet, your personal access token that you have created in your account developer settings. Enter that token as your password.

Assuming you are already using git-credential-osxkeychain, your personal credentials should be stored in your keychain, so two github entries will show up, but with different accounts.

OSX keychain 2 entires for Github

三生池水覆流年 2024-10-03 06:13:35

选项 0:您不想搞乱操作系统设置。您只想为一个存储库使用不同的公钥提交到另一个 github 帐户。

解决方案

  1. 创建新密钥:ssh-keygen -t rsa -b 4096 -f ~/.ssh/alt_rsa

  2. 将密钥添加到密钥集中:ssh-add -K ~/. ssh/alt_rsa

  3. 将 pub 密钥复制并添加到 github 帐户:(请参阅 github 说明)

  4. 使用 github 测试密钥:ssh -i ~/.ssh/alt_rsa T [电子邮件受保护]

  5. 使用 git 协议(不是 HTTP)克隆存储库:git clone git@github:myaccount...

  6. 在克隆的存储库中:

    git config core.sshCommand "ssh -i ~/.ssh/alt_rsa -F /dev/null"
    git config user.name [myaccount]
    git config user.email [我的帐户电子邮件]

  7. 现在你应该能够正确地进行 git 推送,而不会干扰你的日常 git 帐户

Option 0: you dont want to mess around with OS settings.. you just want to commit to a different github account with a different public key for one repo.

solution:

  1. create the new key: ssh-keygen -t rsa -b 4096 -f ~/.ssh/alt_rsa

  2. add the key to the keyset: ssh-add -K ~/.ssh/alt_rsa

  3. copy and add the pub key to the github account: (see github instructions)

  4. test the key with github: ssh -i ~/.ssh/alt_rsa T [email protected]

  5. clone the repo using the git protocol (not HTTP): git clone git@github:myaccount...

  6. in the cloned repo:

    git config core.sshCommand "ssh -i ~/.ssh/alt_rsa -F /dev/null"
    git config user.name [myaccount]
    git config user.email [myaccount email]

  7. now you should be able to git push correctly without interferring with your everyday git account

逆蝶 2024-10-03 06:13:35

在一台 Windows 计算机上管理多个 GitHub 帐户 (HTTPS)

假设您之前在计算机上使用 git 并配置了 git 全局配置文件。要检查它,请打开终端,然后:

git config --global -e

它会打开您的编辑器,您可能会看到类似这样的内容:

[user]
    email = [email protected]
    name = Your_Name
...

这很棒,因为您可以将代码推送到 GitHub 帐户,而无需每次都输入凭据。
但是如果需要从另一个帐户推送到仓库怎么办?在这种情况下,git 将拒绝并显示 403 错误,并且您必须更改全局 git 凭据。为了使这个舒适的 lat 设置在凭证管理器中存储存储库名称:

git config --global credential.github.com.useHttpPath true

再次检查它打开配置
git 配置 --global -e
你会看到新的配置行

[credential]
    useHttpPath = true
...

就是它了。现在,当您第一次推送到任何帐户时,您将看到一个弹出窗口
Screenshot_1

输入此存储库帐户的特定凭据,这将将此帐户“绑定”到存储库。因此,在您的计算机中,您可以根据需要指定任意数量的帐户/存储库。

如需更详细的解释,您可以观看我在 YouTube 上找到的这个很酷的视频:
https://youtu.be/2MGGJtTH0bQ

Manage multiple GitHub accounts on one Windows machine (HTTPS)

Let's say you previously use git on your machine and configure git global config file. To check it open the terminal and :

git config --global -e

It opens your editor, and you may see something like this:

[user]
    email = [email protected]
    name = Your_Name
...

And this is great because you can push your code to GitHub account without entering credentials every time.
But what if it needs to push to repo from another account? In this case, git will reject with 403 err, and you must change your global git credentials. To make this comfortable lat set storing a repo name in a credential manager:

git config --global credential.github.com.useHttpPath true

to check it open config one more time
git config --global -e
you will see new config lines

[credential]
    useHttpPath = true
...

The is it. Now when you first time push to any account you will see a pop-up
Screenshot_1

Enter specific for this repo account credentials, and this will "bind" this account for the repo. And so in your machine, you can specify as many accounts/repos as you want.

For a more expanded explanation you can see this cool video that I found on youtube:
https://youtu.be/2MGGJtTH0bQ

南巷近海 2024-10-03 06:13:35

使用 docker!

这可能不适合每个人,但这是我最终为自己做的。

我所有的开发现在都是在 docker 容器内完成的。我是 vim 用户,但我知道 VSCode 有可以与 docker 容器配合使用的插件。

因此,由于我对不同的项目/语言有不同的 docker 容器,因此我可以在每个容器中使用不同的 git 配置。问题解决了。

Use docker!

This may not be suitable for everyone but this is what I ended up doing for myself.

All my developments are now done inside docker containers. I am a vim user but I know VSCode has plugin to work with docker containers.

So since I have different docker containers for different projects/languages, I can use different git configurations inside each. Problem solved.

乱世争霸 2024-10-03 06:13:35

在您的 ~./gitconfig 中仔细添加:

[url "[email protected]:{my_username}"]
  insteadOf = https://github.com/{my_username}
[url "[email protected]{-my_org_short}:{my_org_name}/"]
  insteadOf = https://github.com/{my_org_name}/
  insteadOf = [email protected]:{my_org_name}/

其中 {-my_org_short} 是可选的,

您可以添加您的组织用户名而不是 {my_org_name}网址
部分取决于您的 github 设置

更多信息位于 此链接< /a>

in your ~./gitconfig CAREFULLY add:

[url "[email protected]:{my_username}"]
  insteadOf = https://github.com/{my_username}
[url "[email protected]{-my_org_short}:{my_org_name}/"]
  insteadOf = https://github.com/{my_org_name}/
  insteadOf = [email protected]:{my_org_name}/

where {-my_org_short} is optional

you may play adding your org username instead of {my_org_name} in the url
section depending on your github setup

more info at this link

樱花细雨 2024-10-03 06:13:35

与其他答案不同,您需要按照几个步骤才能使用同一台计算机上的两个不同github帐户,对我来说它可以在中使用两个步骤。

您只需:

1) 为 ~/.ssh 位置下的每个帐户生成具有不同名称的 SSH 公钥和私钥对,然后

2) 添加生成的 >设置>>下相应帐户的>公钥 SSH 和 GPG 密钥 >> 新 SSH 密钥

要生成 SSH 公钥和私钥对,请使用以下命令:

cd ~/.ssh
ssh-keygen -t rsa -C "[email protected]" -f "id_rsa_WORK"
ssh-keygen -t rsa -C "[email protected]" -f "id_rsa_PERSONAL"

执行上述命令后,将创建 id_rsa_WORKid_rsa_WORK.pub 文件将为您的工作帐户(例如 git.work.com)创建 id_rsa_PERSONALid_rsa_PERSONAL.pub 为您的个人< /strong> 帐户(例如 github.com)。

创建后,复制每个公共 (*.pub) 文件中的内容,并对每个帐户执行第 2 步。

PS:如果您的两个帐户的主机名不同,则无需像其他答案中提到的那样在 ~/.ssh/config 文件下为每个 git 帐户创建主机条目。

Unlike other answers, where you need to follow few steps to use two different github account from same machine, for me it worked in two steps.

You just need to :

1) generate SSH public and private key pair for each of your account under ~/.ssh location with different names and

2) add the generated public keys to the respective account under Settings >> SSH and GPG keys >> New SSH Key.

To generate the SSH public and private key pairs use following command:

cd ~/.ssh
ssh-keygen -t rsa -C "[email protected]" -f "id_rsa_WORK"
ssh-keygen -t rsa -C "[email protected]" -f "id_rsa_PERSONAL"

As a result of above commands, id_rsa_WORK and id_rsa_WORK.pub files will be created for your work account (ex - git.work.com) and id_rsa_PERSONAL and id_rsa_PERSONAL.pub will be created for your personal account (ex - github.com).

Once created, copy the content from each public (*.pub) file and do Step 2 for the each account.

PS : Its not necessary to make an host entry for each git account under ~/.ssh/config file as mentioned in other answers, if hostname of your two accounts are different.

无人接听 2024-10-03 06:13:34

您所需要做的就是使用多个 SSH 密钥对配置 SSH 设置。

第一个链接中的相关步骤:

  1. 生成 SSH 密钥:

    ssh-keygen -t ed25519 -C "[电子邮件受保护]< /a>”
    

    按照提示确定名称,例如id_ed25519_example_company

  2. 将 SSH 公钥从 ~/.ssh/id_ed25519_doe_company.pub 复制到 GitHub 并告诉 ssh 该密钥:

    ssh-add ~/.ssh/id_ed25519_doe_company
    
  3. ~ 中创建一个 config 文件/.ssh 包含以下内容:

    托管 github-doe-company
      主机名 github.com
      用户git
      IdentityFile ~/.ssh/id_ed25519_doe_company
    
  4. 添加您的遥控器:

    git 远程添加源 git@github-doe-company:username/repo.git
    

    或更改使用:

    git 远程 set-url origin git@github-doe-company:username/repo.git
    

此外,如果您使用不同角色使用多个存储库,则需要确保您的各个存储库相应地覆盖用户设置:

设置用户名、电子邮件和 GitHub 令牌 – 覆盖设置个人回购协议
https://help.github.com/articles/设置您的提交电子邮件地址-in-git/

注意:
有些人可能需要不同的电子邮件用于不同的存储库,从 git 2.13 开始,您可以通过编辑位于以下位置的全局配置文件来设置目录上的电子邮件:~/.gitconfig< /code> 像这样使用条件:

[user]
    name = Default Name
    email = [email protected]

[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig

然后你的工作特定配置 ~/work/.gitconfig 看起来像这样:

[user]
    name = Pavan Kataria
    email = [email protected]

谢谢 @alexg 在评论中告知我这一点。

All you need to do is configure your SSH setup with multiple SSH keypairs.

Relevant steps from the first link:

  1. Generate an SSH-key:

    ssh-keygen -t ed25519 -C "[email protected]"
    

    Follow the prompts and decide a name, e.g. id_ed25519_example_company.

  2. 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
    
  3. 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
    
  4. 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
    

Also, if you're working with multiple repositories using different personas, you need to make sure that your individual repositories have the user settings overridden accordingly:

Setting user name, email and GitHub token – Overriding settings for individual repos
https://help.github.com/articles/setting-your-commit-email-address-in-git/

Note:
Some of you may require different emails to be used for different repositories, from git 2.13 you can set the email on a directory basis by editing the global config file found at: ~/.gitconfig using conditionals like so:

[user]
    name = Default Name
    email = [email protected]

[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig

And then your work-specific config ~/work/.gitconfig would look like this:

[user]
    name = Pavan Kataria
    email = [email protected]

Thank you @alexg for informing me of this in the comments.

一世旳自豪 2024-10-03 06:13:34

使用 HTTPS

将远程 url 更改为 https:

git remote set-url origin https://[email protected]/USERNAME/PROJECTNAME.git

就可以了:

git push

当您第一次输入密码时,您可能会看到以下消息:

2021 年 8 月 13 日删除了对密码身份验证的支持。

您必须使用 Github PAT 在 github.com 上进行 HTTPS 推送

您的 PAT 将看起来像一个 40 个字符的字母/数字哈希,就像

zIa03uTnN0TTaReAALT09K33NMBbCfum6lw8TDSb

当您使用此方法推送时,推送时会出现一个对话框:

on Windows 10">

Github 输入密码 PAT对话框 确保提交显示为由 USERNAME 执行,可以设置<a href=用户。该项目的名称和user.email , 也:

git config user.name USERNAME
git config user.email [email protected]

Use HTTPS

change remote url to https:

git remote set-url origin https://[email protected]/USERNAME/PROJECTNAME.git

and you are good to go:

git push

When you first enter your password, you will probably see this message:

Support for password authentication was removed on August 13, 2021.

You must use a Github PAT to do HTTPS pushes on github.com

Your PAT will look like a 40 character letter/number hash, like

zIa03uTnN0TTaReAALT09K33NMBbCfum6lw8TDSb

When you push using this method, you will be presented with a dialog box when you push:

Github enter password PAT dialog on Windows 10

To ensure that the commits appear as performed by USERNAME, one can setup the user.name and user.email for this project, too:

git config user.name USERNAME
git config user.email [email protected]
爱给你人给你 2024-10-03 06:13:34

进入状态

要在单独的 github/bitbucket/whatever 帐户下管理 git 存储库,您只需生成一个新的 SSH 密钥即可。

但在我们开始使用您的第二个身份推送/拉取存储库之前,我们必须让您熟悉情况 – 假设您的系统使用典型的 id_rsaid_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

现在,当我们查看 tree ~/.ssh 时,我们会看到

$ tree ~/.ssh
/Users/you/.ssh
├── known_hosts
├── github-mainuser
├── github-mainuser.pub
├── github-otheruser
└── github-otheruser.pub    

Next,我们需要设置一个 ~/.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 Host 设置为使用您的 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 文件中设置的 Host 名称。这可以正确地将 git 连接到相应的 IdentityFile 并通过 github 正确验证您的身份


完成工作:创建一个新的存储库

好吧,因为鸡生蛋,蛋生鸡,我们我们将考虑在您的辅助帐户上发布新的存储库。这种情况适用于使用辅助 github 帐户创建内容的用户。

假设您已经在本地完成了一些工作,现在准备推送到 github。如果您愿意,可以跟我一起

$ cd ~
$ mkdir somerepo
$ cd somerepo
$ git init

现在配置此存储库以使用您的 身份

$ git config user.name "Mister Manager"
$ git config user.email "[email protected]"

现在进行第一次提交

$ echo "hello world" > readme
$ git add .
$ git commit -m "first commit"

检查提交以查看您的新身份是否使用了 git log

$ git log --pretty="%H %an <%ae>"
f397a7cfbf55d44ffdf87aa24974f0a5001e1921 Mister Manager <[email protected]>

好了,是时候推送到 github 了!由于 github 还不知道我们的新存储库,因此首先转到 github.com/new 并创建您的新存储库– 将其命名为 somerepo

现在,为了将您的存储库配置为使用正确的身份/凭据与 github“对话”,我们添加了一个遥控器。假设您的新帐户的 github 用户名是 someuser ...

$ git remote add origin github.com-otheruser:someuser/somerepo.git

粗体部分绝对重要,它必须主机<我们在您的 ~/.ssh/config 文件中定义的 /code>

最后,推送存储库

$ git push origin master

更新现有存储库以使用新的 SSH 配置

假设您已经有一些存储库已克隆,但现在您想使用新的 SSH 配置。在上面的示例中,我们通过将您之前的 id_rsa/id_rsa.pub 密钥对分配给 Host 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

,我们现在将其更新为(粗体更改),

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 远程的现有存储库将无法使用此身份文件。但别担心,这是一个简单的修复。

要更新任何现有存储库以使用新的 SSH 配置,请使用 set-url 更新存储库的远程源字段 -

$ cd existingrepo
$ git remote set-url origin github.com-mainuser:someuser/existingrepo.git

就是这样。现在您可以push/pull到您想要的内容


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 密钥,这样如果任何一个密钥被泄露,我不必在我使用过该密钥的所有其他地方更新密钥。这就像当您收到 Adob​​e 的通知,称其 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/config 文件 - 显然 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 公钥时,请运行此命令将公钥输出到 stdout – 在需要的地方复制/粘贴

$ ssh-keygen -y -f someuser.pem
ssh-rsa AAAAB3NzaC1yc2EAAAA...

注意,这也是我将密钥添加到任何远程计算机时使用的相同过程。 ssh-rsa AAAA... 值复制到远程的 ~/.ssh/authorized_keys 文件


转换您的 id_rsa/< code>id_rsa.pub 密钥对为 PEM 格式

那么您想驯服您的密钥文件并减少一些文件系统的损坏吗?将您的密钥对转换为单个 PEM 很容易

$ cd ~/.ssh
$ openssl rsa -in id_rsa -outform pem > id_rsa.pem

或者,按照上面的示例,我们将id_rsa -> 重命名为id_rsa -> 。 github-mainuserid_rsa.pub -> github-mainuser.pub – 所以

$ 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==

Getting into shape

To manage a git repo under a separate github/bitbucket/whatever account, you simply need to generate a new SSH key.

But before we can start pushing/pulling repos with your second identity, we gotta get you into shape – Let's assume your system is setup with a typical id_rsa and id_rsa.pub key pair. Right now your tree ~/.ssh looks like this

$ tree ~/.ssh
/Users/you/.ssh
├── known_hosts
├── id_rsa
└── id_rsa.pub

First, name that key pair – adding a descriptive name will help you remember which key is used for which user/remote

# 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

Next, let's generate a new key pair – here I'll name the new key github-otheruser

$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/github-otheruser

Now, when we look at tree ~/.ssh we see

$ tree ~/.ssh
/Users/you/.ssh
├── known_hosts
├── github-mainuser
├── github-mainuser.pub
├── github-otheruser
└── github-otheruser.pub    

Next, we need to setup a ~/.ssh/config file that will define our key configurations. We'll create it with the proper owner-read/write-only permissions

$ (umask 077; touch ~/.ssh/config)

Open that with your favourite editor, and add the following contents

Host github.com
  User git
  IdentityFile ~/.ssh/github-mainuser

Host github.com-otheruser
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-otheruser

Presumably, you'll have some existing repos associated with your primary github identity. For that reason, the "default" github.com Host is setup to use your mainuser key. If you don't want to favour one account over another, I'll show you how to update existing repos on your system to use an updated ssh configuration.


Add your new SSH key to github

Head over to github.com/settings/keys to add your new public key

You can get the public key contents using: copy/paste it to github

$ cat ~/.ssh/github-otheruser.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDBVvWNQ2nO5...

Now your new user identity is all setup – below we'll show you how to use it.


Getting stuff done: cloning a repo

So how does this come together to work with git and github? Well because you can't have a chicken without and egg, we'll look at cloning an existing repo. This situation might apply to you if you have a new github account for your workplace and you were added to a company project.

Let's say github.com/someorg/somerepo already exists and you were added to it – cloning is as easy as

$ git clone github.com-otheruser:someorg/somerepo.git

That bolded portion must match the Host name we setup in your ~/.ssh/config file. That correctly connects git to the corresponding IdentityFile and properly authenticates you with github


Getting stuff done: creating a new repo

Well because you can't have a chicken without and egg, we'll look at publishing a new repo on your secondary account. This situation applies to users that are create new content using their secondary github account.

Let's assume you've already done a little work locally and you're now ready to push to github. You can follow along with me if you'd like

$ cd ~
$ mkdir somerepo
$ cd somerepo
$ git init

Now configure this repo to use your identity

$ git config user.name "Mister Manager"
$ git config user.email "[email protected]"

Now make your first commit

$ echo "hello world" > readme
$ git add .
$ git commit -m "first commit"

Check the commit to see your new identity was used using git log

$ git log --pretty="%H %an <%ae>"
f397a7cfbf55d44ffdf87aa24974f0a5001e1921 Mister Manager <[email protected]>

Alright, time to push to github! Since github doesn't know about our new repo yet, first go to github.com/new and create your new repo – name it somerepo

Now, to configure your repo to "talk" to github using the correct identity/credentials, we have add a remote. Assuming your github username for your new account is someuser ...

$ git remote add origin github.com-otheruser:someuser/somerepo.git

That bolded portion is absolutely critical and it must match the Host that we defined in your ~/.ssh/config file

Lastly, push the repo

$ git push origin master

Update an existing repo to use a new SSH configuration

Say you already have some repo cloned, but now you want to use a new SSH configuration. In the example above, we kept your existing repos in tact by assigning your previous id_rsa/id_rsa.pub key pair to Host github.com in your SSH config file. There's nothing wrong with this, but I have at least 5 github configurations now and I don't like thinking of one of them as the "default" configuration – I'd rather be explicit about each one.

Before we had this

Host github.com
  User git
  IdentityFile ~/.ssh/github-mainuser

Host github.com-otheruser
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-otheruser

So we will now update that to this (changes in bold)

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

But now any existing repo with a github.com remote will not work with this identity file. But don't worry, it's a simple fix.

To update any existing repo to use your new SSH configuration, update the repo's remote origin field using set-url -

$ cd existingrepo
$ git remote set-url origin github.com-mainuser:someuser/existingrepo.git

That's it. Now you can push/pull to your heart's content


SSH key file permissions

If you're running into trouble with your public keys not working correctly, SSH is quite strict on the file permissions allowed on your ~/.ssh directory and corresponding key files

As a rule of thumb, any directories should be 700 and any files should be 600 - this means they are owner-read/write-only – no other group/user can read/write them

$ 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

How I manage my SSH keys

I manage separate SSH keys for every host I connect to, such that if any one key is ever compromised, I don't have to update keys on every other place I've used that key. This is like when you get that notification from Adobe that 150 million of their users' information was stolen – now you have to cancel that credit card and update every service that depends on it – what a nuisance.

Here's what my ~/.ssh directory looks like: I have one .pem key for each user, in a folder for each domain I connect to. I use .pem keys to so I only need one file per key.

$ 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

And here's my corresponding /.ssh/config file – obviously the github stuff is relevant to answering this question about github, but this answer aims to equip you with the knowledge to manage your ssh identities on any number of services/machines.

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

Getting your SSH public key from a PEM key

Above you noticed that I only have one file for each key. When I need to provide a public key, I simply generate it as needed.

So when github asks for your ssh public key, run this command to output the public key to stdout – copy/paste where needed

$ ssh-keygen -y -f someuser.pem
ssh-rsa AAAAB3NzaC1yc2EAAAA...

Note, this is also the same process I use for adding my key to any remote machine. The ssh-rsa AAAA... value is copied to the remote's ~/.ssh/authorized_keys file


Converting your id_rsa/id_rsa.pub key pairs to PEM format

So you want to tame you key files and cut down on some file system cruft? Converting your key pair to a single PEM is easy

$ cd ~/.ssh
$ openssl rsa -in id_rsa -outform pem > id_rsa.pem

Or, following along with our examples above, we renamed id_rsa -> github-mainuser and id_rsa.pub -> github-mainuser.pub – so

$ cd ~/.ssh
$ openssl rsa -in github-mainuser -outform pem > github-mainuser.pem

Now just to make sure that we've converted this correct, you will want to verify that the generated public key matches your old public key

# 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==

Now that you have your github-mainuser.pem file, you can safely delete your old github-mainuser and github-mainuser.pub files – only the PEM file is necessary; just generate the public key whenever you need it ^_^


Creating PEM keys from scratch

You don't need to create the private/public key pair and then convert to a single PEM key. You can create the PEM key directly.

Let's create a newuser.pem

$ openssl genrsa -out ~/.ssh/newuser.pem 4096

Getting the SSH public key is the same

$ ssh-keygen -y -f ~/.ssh/newuser.pem
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACA ... FUNZvoKPRQ==
萌逼全场 2024-10-03 06:13:34

通过在 ~/.ssh/config 中为 github.com 创建不同的主机别名,
并为每个主机别名提供自己的 ssh 密钥,您可以轻松地使用多个
github 帐户不会混淆。
那是因为 github.com 不区分用户,它总是只是 git,
但通过您用来连接的 ssh 密钥。
只需使用您自己的主机别名配置您的远程源即可。”

上面的总结来自于下面博客文章的评论。

我发现这个解释最清楚。它对我有用,至少从 2012 年 4 月开始。

http://net.tutsplus.com/tutorials/tools-and-tips/how-to-work-with-github-and-multiple-accounts/

By creating different host aliases to github.com in your ~/.ssh/config,
and giving each host alias its own ssh key, you can easily use multiple
github accounts without confusion.
That’s because github.com distinguishes not by user, which is always just git,
but by the ssh key you used to connect.
Just configure your remote origins using your own host aliases.”

The above summary is courtesy of comments on the blog post below.

I've found this explanation the clearest. And it works for me, at least as of April 2012.

http://net.tutsplus.com/tutorials/tools-and-tips/how-to-work-with-github-and-multiple-accounts/

等风也等你 2024-10-03 06:13:34

详细信息位于 http mishaba 链接的://net.tutsplus.com/tutorials/tools-and-tips/how-to-work-with-github-and-multiple-accounts/ 对我来说效果很好。

从该页面:

$ touch ~/.ssh/config

然后将该文件编辑为如下所示(每个帐户一个条目):

#Default GitHub
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

Host github-COMPANY
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_COMPANY

The details at http://net.tutsplus.com/tutorials/tools-and-tips/how-to-work-with-github-and-multiple-accounts/ linked to by mishaba work very well for me.

From that page:

$ touch ~/.ssh/config

Then edit that file to be something like this (one entry per account):

#Default GitHub
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

Host github-COMPANY
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_COMPANY
千里故人稀 2024-10-03 06:13:34

就我而言,我在 Git-Lab 中有工作帐户,在 GitHub 中有个人帐户。
我的 Git-Lab 帐户已全局配置为可以从我的笔记本电脑中的所有目录进行访问,如下所示:

git config --global user.name WORK_USERNAME
git config --global user.email [email protected]

因此,如果您正在寻找不使用 SSL 的解决方案,您可以通过在空文件夹,然后在该文件夹中插入您的个人帐户凭据:

git config user.name PERSONAL_USERNAME
git config user.email [email protected]

请注意,此处未设置 --global ,因为您只希望从那里访问您的个人 git,而不是在任何地方访问,因此无论其中的内容如何文件夹的 git 凭据将连接到您的个人帐户,而其外部将连接到您的工作帐户。

之后,您可以像 git clone your_repo_link.git 一样克隆您的存储库。然后会弹出一个新窗口并要求您使用 github 帐户登录。

要验证您的过程,请在您创建的文件夹中尝试 git config --list,您应该会看到工作和个人用户名以及电子邮件及其目录。

在该文件夹之外,如果运行 git config --list ,您应该只能看到您的工作用户名和电子邮件。

就是这样,希望这对没有配置 SSL 的人有所帮助。

In my case, I have my work account in Git-Lab and my personal account in GitHub.
My Git-Lab account was configured globally to be accessed from all directories in my laptop like this:

git config --global user.name WORK_USERNAME
git config --global user.email [email protected]

So if you're looking for a solution without using SSL, you can do it by using git init in an empty folder, and then insert your personal account credentials in that folder:

git config user.name PERSONAL_USERNAME
git config user.email [email protected]

Notice here --global is not set, because you only want your personal git to be accessed from there and not everywhere, so whatever inside that folder will have its git credentials connected to your personal account and outside of it will be connected to your work account.

After that, you can clone your repository like so git clone your_repo_link.git. Then a new window will popup and ask you to login with your github account.

To verify your process try git config --list inside the folder that you created, you should see both work and personal usernames and emails with their directories.

Outside that folder, if you run git config --list you should see your work username and email only.

That's it, hope this helps someone without configuring SSL.

来世叙缘 2024-10-03 06:13:34
  • 生成 ssh 密钥

    ssh-keygen -t ed25519 -C“您的电子邮件”
    
  • 从 .ssh 文件夹复制公钥并添加到您的 git-repository 帐户

  • 转到 ~/.ssh

  • 创建一个名为 config 的文件(没有扩展名)

  • 打开配置文件&添加以下代码。 (根据您的帐户进行更改)

  1. 帐户1

    <前><代码> # account_1
    托管 gitlab.com-account_1
    主机名 gitlab.com
    用户git
    PreferredAuthentications 公钥
    身份文件 ~/.ssh/id_rsa_account_1

  2. 帐户 2

    <前><代码> # 帐户2
    托管 gitlab.com-Account2
    主机名 gitlab.com
    用户git
    PreferredAuthentications 公钥
    身份文件 ~/.ssh/id_rsa_Account2

  3. 帐户 3

    <前><代码> # Account_3
    托管 github.com-Account3
    主机名 github.com
    用户git
    PreferredAuthentications 公钥
    身份文件 ~/.ssh/id_rsa_Account_3

  • 添加远程 URL,如下
  1. 帐户 1

     git Remote add origin [电子邮件受保护]_1:组名/repo_name.git
    
  2. 帐户 2

     git Remote add origin [电子邮件受保护]:group_name /repo_name.git
    
  3. 帐户 3

     git Remote add origin github.com-Account3:github_username/repo_name.git
    

确保 IdentityFile 名称与您在 ssh 密钥生成期间创建的名称相同。

  • Generate ssh keys

    ssh-keygen -t ed25519 -C "your_email"
    
  • Copy the public key from .ssh folder and add to your git-repository account

  • Go to ~/.ssh

  • Create a file named config(have no extension )

  • Open config file & add below codes. (change according to your account)

  1. Account 1

        # account_1
        Host gitlab.com-account_1
        HostName gitlab.com
        User git
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/id_rsa_account_1
    
  2. Account 2

        # Account2
        Host gitlab.com-Account2
        HostName gitlab.com
        User git
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/id_rsa_Account2
    
  3. Account 3

        # Account_3
        Host github.com-Account3
        HostName github.com
        User git
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/id_rsa_Account_3
    
  • Add remote url as follows
  1. Account 1

         git remote add origin [email protected]_1:group_name/repo_name.git
    
  2. Account 2

         git remote add origin [email protected]:group_name/repo_name.git
    
  3. Account 3

         git remote add origin github.com-Account3:github_username/repo_name.git
    

Make sure that IdentityFile names are same as you created during ssh key generation.

奢华的一滴泪 2024-10-03 06:13:34

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

  1. 检查现有 ssh 密钥: 打开终端并运行此命令以查看/列出现有 ssh 密钥 ls -al ~ /.ssh
    扩展名为 .pub 的文件是您的 ssh 密钥,因此您应该有两个用于个人工作账户。如果只有一个或没有,则需要生成其他方式,请跳过此操作。

    - 生成 ssh 密钥:登录 github(个人或工作帐户),导航到设置并复制关联的电子邮件。
    现在返回终端并运行ssh-keygen -t rsa -C "the Copyed email",您将看到:

    正在生成公共/私有 rsa 密钥对。
    输入要保存密钥的文件 (/.../.ssh/id_rsa):

    id_rsa 是即将生成的 ssh 密钥的默认名称因此,复制路径并重命名默认值,例如 /.../.ssh/id_rsa_work (如果为工作帐户生成)。提供密码或直接输入忽略,您将看到类似 The key's randomart image is: 和图像。完成。
    为您的第二个 github 帐户再次重复此步骤。确保您使用正确的电子邮件地址和不同的 ssh 密钥名称(例如 id_rsa_personal)以避免覆盖。
    在此阶段,运行ls -al ~/.ssh 再次。

  2. 将 ssh 密钥与 gitHub 帐户关联:下一步是复制其中一个 ssh 密钥,运行此命令但替换您自己的 ssh 密钥名称:pbcopy < ~/.ssh/id_rsa_work.pub,将 id_rsa_work.pub 替换为您自己的。
    现在我们的 ssh 密钥已复制到剪贴板,返回 github 帐户 [Make如果您复制的 ssh 密钥是 id_rsa_work,请确保您已登录工作帐户],然后导航至
    设置 - SSH 和 GPG 密钥并点击新 SSH 密钥按钮(不是新 GPG 密钥顺便说一句:D)
    为此密钥提供一些标题,粘贴密钥并单击“添加 SSH 密钥”。您现在要么已成功添加 ssh 密钥,要么注意到它一直存在,这很好(或者您收到错误,因为您选择了新 GPG 密钥,而不是新 SSH 密钥:D)。
  3. 将 ssh 密钥与 gitHub 帐户关联:对您的第二个帐户重复上述步骤。
  4. 编辑全局 git 配置:最后一步是确保全局配置文件了解所有 github 帐户(这么说)。
    运行 git config --global --edit 编辑这个全局文件,如果这打开了vim并且你不知道如何使用它,请按i进入插入模式,如下编辑文件,然后按esc 后跟 :wq 退出插入模式:

    [在方括号内给出后面的 acc 的名称。]
    名称 = github_用户名
    电子邮件 = github_电子邮件地址
    [任何其他名称]
    名称 = github_用户名
    电子邮件 = github_email
    [凭据]
    助手 = osxkeychain
    useHttpPath = true

完成!,现在当尝试从存储库推送或拉取时,系统会询问您哪个 GitHub 帐户应与此存储库链接,并且仅询问一次,本地配置将记住此链接而不是全局配置,以便您可以处理与不同帐户链接的不同存储库,而无需每次都编辑全局配置。

This answer is for beginners (none-git gurus). I recently had this problem and maybe its just me but most of the answers seemed to require rather advance understanding of git. After reading several stack overflow answers including this thread, here are the steps I needed to take in order to easily switch between GitHub accounts (e.g. assume two GitHub accounts, github.com/personal and gitHub.com/work):

  1. 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.

  2. 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 to
    Settings - 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).
  3. Associate ssh key with gitHub account: Repeat the above step for your second account.
  4. 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

Done!, now when trying to push or pull from a repo, you'll be asked which GitHub account should be linked with this repo and its asked only once, the local configuration will remember this link and not the global configuration so you can work on different repos that are linked with different accounts without having to edit global configuration each time.

一梦浮鱼 2024-10-03 06:13:34

刚刚为 Windows 解决了这个问题,使用每个存储库的凭据:

cd c:\User1\SomeRepo
git config --local credential.https://github.com.user1 user1
git config --local credential.useHttpPath true
git config --local credential.helper manager
git remote set-url origin https://[email protected]/USERNAME/PROJECTNAME.git

凭据的格式。https://github.com。告诉凭证助手凭证的 URL。
“useHttpPath”告诉凭证管理器使用凭证的路径。如果省略 useHttpPath,则凭证管理器将为 https://github.com 存储一个凭证。如果包含它,那么凭证管理器将存储多个凭证,这正是我真正想要的。

just figured this out for Windows, using credentials for each repo:

cd c:\User1\SomeRepo
git config --local credential.https://github.com.user1 user1
git config --local credential.useHttpPath true
git config --local credential.helper manager
git remote set-url origin https://[email protected]/USERNAME/PROJECTNAME.git

The format of credential.https://github.com. tells the credential helper the URL for the credential.
The 'useHttpPath' tells the credential manager to use the path for the credential. If useHttpPath is omitted then the credential manager will store one credential for https://github.com. If it is included then the credential manager will store multiple credentials, which is what I really wanted.

弱骨蛰伏 2024-10-03 06:13:34

我使用 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),否则它不会构建我的项目:

运行脚本放置在依赖项之后(使用 /bin/ksh 作为 shell):

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

编辑:添加了对新文件存在的测试并将旧文件复制到 /tmp 以解决下面 @naomik 的评论。

I use shell scripts to switch me to whatever account I want to be "active". Essentially you start from a fresh start, get one account configured properly and working, then move the these files to a name with the proper prefix. From then on you can use the command "github", or "gitxyz" to switch:

# 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

I've had great luck with this. I also created a run script in Xcode (for you Mac users) so it would not build my project unless I had the proper setting (since its using git):

Run Script placed after Dependencies (using /bin/ksh as the shell):

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

EDIT: added tests for new files existence and copying old files to /tmp to address comment by @naomik below.

送君千里 2024-10-03 06:13:34

更简单、更容易的修复以避免混淆。Windows

用户可以为不同的项目使用多个或不同的 git 帐户。

以下步骤:
转到控制面板并搜索凭据管理器。
然后转到凭证管理器 -> Windows 凭据

现在删除通用凭据标题下的 git:https://github.com 节点

这将删除当前凭据。现在你可以通过 git pull 添加任何项目,它会要求用户名和密码。

当您遇到其他帐户的任何问题时,请执行相同的过程。

参考图片

Simpler and Easy fix to avoid confusion..

For Windows users to use multiple or different git accounts for different projects.

Following steps:
Go Control Panel and Search for Credential Manager.
Then Go to Credential Manager -> Windows Credentials

Now remove the git:https//github.com node under Generic Credentials Heading

This will remove the current credentials. Now you can add any project through git pull it will ask for username and password.

When you face any issue with other account do the same process.

refer to image

毅然前行 2024-10-03 06:13:34

有很多答案解释了如何完成您所要求的任务,但我想提供不同的观点。

我认为将工作和个人事务分开是有道理的。在所有操作系统上的用户帐户之间切换非常简单,因此您只需为工作和娱乐创建单独的操作系统帐户,并在每个帐户中登录不同的 github 帐户即可。

保持明确的关注点分离还可以...

  • 防止您的工作文件与个人文件混合在一起
  • 降低对错误资源执行操作的风险
  • 潜在地将安全漏洞限制在一组帐户/凭据

There are plenty of answers explaining how to accomplish what you have asked but I'd like to offer a different perspective.

I think there's something to be said for keeping work and personal stuff separate from each other. It's pretty trivial to switch between user accounts on all operating systems so you could just create separate OS accounts for work and play and have different github accounts logged in in each.

Maintaining a clear separation of concerns can also...

  • prevent your work files becoming co-mingled with your personal files
  • reduce the risk of carrying out an action on the wrong resource
  • potentially confine security breaches to one set of accounts / credentials
心的位置 2024-10-03 06:13:34

如果您不想弄乱此处提到的 ~/.ssh/config 文件,您可以运行 git config core.sshCommand "ssh -i ~/.ssh/custom_id_rsa " 在您想要从不同帐户提交的存储库中。

其余设置相同:

  1. 使用 ssh-keygen -t rsa -f ~/.ssh -f ~/.ssh/custom_id_rsa 为第二个帐户创建新的 SSH 密钥< /p>

  2. 使用您的其他帐户登录 github帐户,转到 https://github.com/settings/keys ,然后粘贴 ~/.ssh/custom_id_rsa.pub

  3. 确保您使用的是 SSH 而不是 HTTPS作为远程 url:git remote set-url origin [电子邮件受保护]:upstream_project_teamname/upstream_project.git

In case you don't want to mess with the ~/.ssh/config file mentioned here, you could instead run git config core.sshCommand "ssh -i ~/.ssh/custom_id_rsa" in the repo where you want to commit from a different account.

The rest of the setup is the same:

  1. Create a new SSH key for the second account with ssh-keygen -t rsa -f ~/.ssh -f ~/.ssh/custom_id_rsa

  2. Sign in to github with your other account, go to https://github.com/settings/keys , and paste the contents of ~/.ssh/custom_id_rsa.pub

  3. Make sure you're using SSH instead of HTTPS as remote url: git remote set-url origin [email protected]:upstream_project_teamname/upstream_project.git

酷到爆炸 2024-10-03 06:13:34

我在这里看到了很多可能的解决方法。作为一种快速解决方案,@Greg 的解决方案似乎相对简单。但是,从长远来看,最好为所有不同的帐户设置单独的 ssh 密钥。此视频对其进行了简要演示。以下是 视频博客

第 1 步 - 为新帐户创建新的 SSH 密钥并将其保存在单独的文件中(例如 ~/.ssh/id_rsa_new_account_name),而不是原始文件,即 ~/.ssh/ id_rsa

ssh-keygen -t rsa -C "your-email-address"

第 2 步 - 附加新密钥

  • 接下来,登录您的第二个 GitHub 帐户,
  • 浏览到帐户概览,然后附加新密钥 ~/.id_rsa SSH 公钥 部分中的 ssh/id_rsa_new_account_name.pub 。
  • 在终端中,输入 ssh-add ~/.ssh/id_rsa_new_account_name 告诉 SSH 添加新身份。如果成功,您将看到已添加身份的响应。

第 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

第 4 步 - 尝试一下
现在,每次您想要使用新帐户和关联的存储库时,请为相应的存储库键入此内容

git remote add origin git@github-new-account-name:your-domain.com/your-repo.git

I see a lot of possible workarounds here. As a quick fix, @Greg's one seems relatively easy. However, it's best to set up separate ssh keys for all different accounts in the long run. This video demonstrates it briefly. Here are the steps mentioned in that video blog.

Step 1 - Create a New SSH Key for a new account and save it in a separate file (e.g. ~/.ssh/id_rsa_new_account_name), not in the original file i.e. ~/.ssh/id_rsa

ssh-keygen -t rsa -C "your-email-address"

Step 2 - Attach the New Key

  • Next, login to your second GitHub account
  • Browse to Account Overview, and attach the new key ~/.ssh/id_rsa_new_account_name.pub within the SSH Public Keys section.
  • In the Terminal, tell SSH to add the new identity by typing ssh-add ~/.ssh/id_rsa_new_account_name. If successful, you'll see a response of Identity Added.

Step 3 - Create a Config File

touch ~/.ssh/config

And save the following contents into the file

#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

Step 4 - Try it Out
Now every time you want to use the new account and the associated repo, type this for the respective repo

git remote add origin git@github-new-account-name:your-domain.com/your-repo.git
憧憬巴黎街头的黎明 2024-10-03 06:13:34

最简单直接的方法(恕我直言)——没有配置文件,没有太多麻烦,

只需创建另一个 ssh 密钥。

假设您有一个新的工作 GitHub 帐户,只需为其创建一个新密钥:

ssh-keygen -t rsa -C "email@work_mail.com" -f "id_rsa_work_user1"`

您只需运行上述一次。

现在您应该拥有旧的和新的,要查看它们,请运行:

ls -al ~/.ssh

从现在开始,每次您想在两者之间切换时,只需运行:

ssh-add -D
ssh-add ~/.ssh/id_rsa_work_user1 #make to use this without the suffix .pub

为了切换到旧的,请再次运行:

 ssh-add -D
 ssh-add ~/.ssh/<previous id_rsa>

The easiest and straightforward approach (IMHO) - no config files not too much hassle

Just create another ssh key.

Let's say you have a new work GitHub account, just create a new key for it:

ssh-keygen -t rsa -C "email@work_mail.com" -f "id_rsa_work_user1"`

You need to run the above only once.

Now you should have the old one and the new one, to see them, run:

ls -al ~/.ssh

From now on, every time you want to switch between the two, just run:

ssh-add -D
ssh-add ~/.ssh/id_rsa_work_user1 #make to use this without the suffix .pub

In order the switch to the old one, run again:

 ssh-add -D
 ssh-add ~/.ssh/<previous id_rsa>
暮凉 2024-10-03 06:13:34

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

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

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

Beside of creating multiple SSH Keys for multiple accounts you can also consider to add collaborators on each project using the same account emails and store the password permanently.

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

I have setup multiple accounts with different emails then put the same user and email on each account as one of the collaborators. By this way I can access to all account without adding SSH Key, or switching to another username, and email for the authentication.

硪扪都還晓 2024-10-03 06:13:34

使用 SSH 密钥对让我的私人存储库正常工作。这是在 Windows 的 git 上进行测试的。

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

A. 生成公钥和私钥

  1. 对 启动 git bash
  2. 运行 ssh-keygen -t ed25519 -C "[电子邮件受保护]"
  3. 当系统提示您“输入其中包含以下内容的文件”时保存密钥”,按 Enter 接受默认值。
  4. 按 Enter 键输入空白密码。
  5. 启动 ssh 代理:eval $(ssh-agent)
  6. 将私钥添加到 ssh 代理并存储密码:ssh-add ~/.ssh/id_ed25519

B. 添加 SSH GitHub 帐户密钥

  1. 将公钥复制到剪贴板: clip < ~/.ssh/id_ed25519.pub
  2. 在 GitHub 上,转到个人资料 ->设置-> SSH 密钥 ->新的 SSH 密钥
  3. 给出一个标题。例如“MacBook Pro 上的 Windows”
  4. 粘贴密钥并点击“添加 SSH 密钥”。

C. 测试 SSH 连接

  1. 输入:ssh -T [email protected]
  2. 对于任何警告消息,请点击“是”。
  3. 它应该显示:“嗨用户名!...”表示测试成功。

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

  1. 更改电子邮件和用户名:
git config user.email [email protected]
git config user.name github_username
  1. 更新远程链接以使用 git。首先列出远程 URI:
git remote -v
git remote set-url origin [email protected]:github_username/your-repo-name.git

E. 测试

git remote show origin

Got my private repo working using SSH key pairs. This was tested on git for Windows.

Source: 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. Generate public and private key pairs

  1. Start git bash
  2. Run ssh-keygen -t ed25519 -C "[email protected]"
  3. When you're prompted to "Enter a file in which to save the key," press Enter to accept the default.
  4. Press enter for a blank passphrase.
  5. Start the ssh agent: eval $(ssh-agent)
  6. Add private key to ssh agent and store the passphrase: ssh-add ~/.ssh/id_ed25519

B. Add SSH keys to GitHub account

  1. Copy the public key to the clipboard: clip < ~/.ssh/id_ed25519.pub
  2. On GitHub, go to Profile -> Settings -> SSH Keys -> New SSH Key
  3. Give a title. E.g. "Windows on MacBook Pro"
  4. Paste the key and hit "Add SSH Key".

C. Test SSH connection

  1. Enter: ssh -T [email protected]
  2. Hit "yes" for any warning message.
  3. It should show: "Hi username!..." indicating a successful test.

D. Setup local repository to use SSH keys

  1. Change email and user name:
git config user.email [email protected]
git config user.name github_username
  1. Update remote links to use git. First list remote URI's:
git remote -v
git remote set-url origin [email protected]:github_username/your-repo-name.git

E. Test

git remote show origin
黎歌 2024-10-03 06:13:34

我发现这个 gem 非常有用:sshwitch

https://github.com/agush22/sshwitch
http://rubygems.org/gems/sshwitch

它有助于切换 ssh 密钥。请记住先备份所有内容!

另外,为了确保提交具有与其关联的正确电子邮件地址,我确保 ~/.gitconfig 文件具有正确的电子邮件地址。

I found this gem to be very useful: sshwitch

https://github.com/agush22/sshwitch
http://rubygems.org/gems/sshwitch

It helps to switch out ssh keys. Remember to back up everything first!

Also to make sure that commits have the correct email address associated with them, I made sure that the ~/.gitconfig file had the proper email address.

谎言 2024-10-03 06:13:34

另一种更简单的方法是使用多个桌面应用程序,就像我正在做的那样,在 Github 桌面上使用帐户 A,同时在 Github Kraken 上使用帐户 B

another easier way is using multiple desktop apps, like what i am doing, using account A on Github desktop, while using account B on Github Kraken

月寒剑心 2024-10-03 06:13:34

无需处理 SSH 密钥对的简单方法

只需更改 user.name & user.email 位于存储库本地。到那些你想要推送到该仓库的人。

示例:我在 gitlab 中有一个工作帐户,有一个项目。克隆此项目/存储库后,在终端中输入:

git config user.name "my-work-username"
git config user.email "my-work-email-id"

现在,假设我在 Gitlab 中有另一个个人项目/存储库,我想将其与我的个人帐户关联。就像上面一样,克隆后,我输入:

git config user.name "my-personal-username"
git config user.email "my-personal-email-id"

希望这有帮助。如果对你有用就点个赞吧! :)

Simple approach without dealing with SSH keypairs

Just change the user.name & user.email locally in the repo. to the ones you want to push to that repo with.

Example: I have a work account in gitlab having a project. After cloning this project/repo, in the terminal, I type out:

git config user.name "my-work-username"
git config user.email "my-work-email-id"

Now, say I've another personal project/repo in Gitlab that I want to associate with my personal account. Just like above, after cloning, I type:

git config user.name "my-personal-username"
git config user.email "my-personal-email-id"

Hope this helps. Upvote if it worked for you! :)

当爱已成负担 2024-10-03 06:13:34

只需将这一行添加到您最喜欢的编辑器中,您就完成了一生

git remote set-url origin https://[email protected]/profile-name/repo-name

just add this line in your fav editor and you are done for life

git remote set-url origin https://[email protected]/profile-name/repo-name
养猫人 2024-10-03 06:13:34

2023 更新:

您可以使用 Github Desktop。在这里,您只需在本地克隆存储库并使用 Github Desktop 管理代码即可。

2023 Update:

You can use Github Desktop. Here you just have to clone the repository in your local and manage the code using Github Desktop.

背叛残局 2024-10-03 06:13:34

如果您碰巧安装了 WSL,您可以拥有两个单独的git 帐户 - 一个在 WSL 上,一个在 Windows 上。

If you happen to have WSL installed you can have two seperate git accounts - one on WSL and one in windows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文