简体   繁体   English

如何在终端 (WSL2) 中在同一设备上的两个 git 帐户之间切换?

[英]How to switch between two git accounts on same device, in the Terminal (WSL2)?

I have created two separate GitHub accounts.我创建了两个单独的 GitHub 帐户。

I have also generated two separate SSH keys for them too, using the same device.我还使用相同的设备为他们生成了两个单独的 SSH 密钥。

When I typed git config user.name in the terminal, the output shows USERNAME2 .当我在终端中输入git config user.name时,输出显示USERNAME2

I want to work in account 1 right now, so I typed git config --global user.name "USERNAME1 and did the same for the email.我现在想在帐户 1 中工作,所以我输入了git config --global user.name "USERNAME1并对电子邮件执行了相同的操作。

But I think that it hasn't truly switched accounts and just renamed my username or something?但我认为它并没有真正切换帐户,只是重命名了我的用户名之类的? The reason I think that is because when I'm trying to create a new repo in account, for ALL git commands, I keep getting a fatal: not a git repository (or any of the parent directories): .git error, which I never got before now, when I had a single account running.我认为这是因为当我试图在帐户中创建一个新的 repo 时,对于所有 git 命令,我不断收到一个fatal: not a git repository (or any of the parent directories): .git错误,我以前从未有过,当时我只有一个帐户在运行。

My SSH keys are in two different folders (for account1, it's in .ssh in the home dir, and for account2, it's in .newssh folder in home dir).我的 SSH 密钥位于两个不同的文件夹中(对于 account1,它位于主目录中的 .ssh 中,而对于 account2,它位于主目录中的 .newssh 文件夹中)。

What do I do?我该怎么办?

I want to work in account 1 right now, so I typed git config --global user.name "USERNAME1"我现在想在账户 1 中工作,所以我输入了git config --global user.name "USERNAME1"

That has nothing to do with remote Git server authentication.这与远程 Git 服务器身份验证无关。
It only sets local commit authorship.它只设置本地提交作者。

To use different accounts, you need different remote URLs, each URL set to use the proper private ssh key for remote authentication.要使用不同的帐户,您需要不同的远程 URL,每个 URL 设置为使用正确的私有 ssh 密钥进行远程身份验证。

This is done with ~/.ssh/config , as shown here .这是通过~/.ssh/config如下所示

Host github2
HostName github.com
User git
IdentityFile /home/me/.newssh/key2

And it supposes:它假设:

  • you have git init myProjectFolder , in order to work within ProjectFolder你有git init myProjectFolder ,以便在ProjectFolder工作

  • you have set the correct remote URL in the Projectfolder您已在Projectfolder设置了正确的远程 URL

     git remote add origin github2:<me>/myProject

Step-by-step, assuming an active shell session in WSL2 (do change the names by something more meaningful to you)一步一步,假设 WSL2 中的 shell 会话处于活动状态(请更改对您更有意义的名称)

  1. generate SSH keys, one per account生成 SSH 密钥,每个帐户一个
cd ~/.ssh
ssh-keygen -t rsa -P "" -f account1
ssh-keygen -t rsa -P "" -f account2
  1. Register account1.pub and accuont2.pub to their respective GitHub account: see " Adding a new SSH key to your GitHub account "将 account1.pub 和 accunt2.pub 注册到各自的 GitHub 帐户:请参阅“ 将新的 SSH 密钥添加到您的 GitHub 帐户

  2. Setup ~/.ssh/config with:使用以下命令设置~/.ssh/config

Host account1
  HostName github.com
  User git
  IdentityFile ~/.ssh/account1
Host account2
  HostName github.com
  User git
  IdentityFile ~/.ssh/account2
  1. Clone your repositories using the right URL (and with it, the right account)使用正确的 URL(并使用正确的帐户)克隆您的存储库
cd
git clone account1:<NameAccount1>/<Repo1>
git clone account1:<NameAccount2>/<Repo2>

( <...> are placeholder values, meant to be replaced: do not keep < and > ) <...>是占位符值,意在被替换:不要保留<>

  1. In each repository, set the right commit authroship在每个存储库中,设置正确的提交权限
cd ~/<repo1>
git config user.name <NameAccount1>
git config user.email <NameAccount1@email.com> # the one registered with GitHub

cd ~/<repo2>
git config user.name <NameAccount2>
git config user.email <NameAccount2@email.com> # the one registered with GitHub

After those steps, each commit will be done with the right GitHub account, and push to the right GitHub repository.在这些步骤之后,每次提交都将使用正确的 GitHub 帐户完成,并推送到正确的 GitHub 存储库。

您只能使用一个 ssh 密钥,只需将 pub 密钥添加到两个 github 帐户即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM