我现在想在账户 1 中工作,所以我输入了git config --global user.name "USERNAME1"
这与远程 Git 服务器身份验证无关。
它只设置本地提交作者身份。
要使用不同的帐户,您需要不同的远程 URL,每个 URL 都设置为使用正确的私有 ssh 密钥进行远程身份验证。
这是通过~/.ssh/config、as shown here 完成的。
Host github2
HostName github.com
User git
IdentityFile /home/me/.newssh/key2
它假设:
-
您拥有git init myProjectFolder,以便在ProjectFolder 内工作
-
您在Projectfolder 中设置了正确的远程 URL
git remote add origin github2:<me>/myProject
一步一步,假设在 WSL2 中有一个活动的 shell 会话(将名称更改为对您更有意义的名称)
- 生成 SSH 密钥,每个帐户一个
cd ~/.ssh
ssh-keygen -t rsa -P "" -f account1
ssh-keygen -t rsa -P "" -f account2
-
将 account1.pub 和 accuont2.pub 注册到各自的 GitHub 帐户:参见“Adding a new SSH key to your GitHub account”
-
设置~/.ssh/config:
Host account1
HostName github.com
User git
IdentityFile ~/.ssh/account1
Host account2
HostName github.com
User git
IdentityFile ~/.ssh/account2
- 使用正确的 URL(以及正确的帐户)克隆您的存储库
cd
git clone account1:<NameAccount1>/<Repo1>
git clone account1:<NameAccount2>/<Repo2>
(<...> 是占位符值,要被替换:不要保留< 和>)
- 在每个存储库中,设置正确的提交权限
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
在这些步骤之后,每次提交都将使用正确的 GitHub 帐户完成,并推送到正确的 GitHub 存储库。