在一台计算机上的一个 Git 中切换用户标识

我在 GitHub上有 存储库,我们称之为 Repo-1

我想首先以 默认的 Git 用户的形式访问该存储库。

让我们把这个用户称为 User-1

我创建了 SSH 密钥对一切都很好,效果不错


我在 GitHub上建立了 再来一杯仓库,我们称之为 Repo-2

在本地 Git 中没有做任何更改,在我的笔记本上,没有结构变化,什么都没有。

现在-我想从 Repo-1克隆人作为 User-2(但是 同一台笔记本)。

首先: 这有可能做到吗?

本地 Git 可以在一台笔记本电脑上在“用户帐户”之间切换并显示为 User-2吗?然后,从那个身份,克隆从回收 -1,作出一些改变,然后推到 Repo-1

如果可能的话,我该怎么做?

146956 次浏览

You have your global .gitconfig where you already configured your SSH Keys/User Information. The global .gitconfig is overridden by a local gitconfig - the file "config" in your .git folder (if it does not exist you might have to create it).

For example you can copy the .gitconfig file into the .git folder (and rename it to "config") and just change the lines you want to change (probably github.user and github.token) or you create a new file with just the two lines in it.

If you prefer the command line "git config" you can avoid all the file moving stuff by omitting the "--global" option.

You need to determine if you actually have two ssh keypairs, or just two emails you want to use. An ssh keypair is linked to accounts as described here.

The ssh keypair (specifically the private key), basically gives your git client permission to connect to github, and thus permission to push. This is separate from the user identity, which is just the email in your commit messages.

If you have two ssh keypairs, each linked to one account, follow these instructions to create a ~/.ssh/config file. The key part is to use a different ssh psuedo-host for each account:

# Default GitHub user (joe)
Host github.com
HostName github.com
User git
IdentityFile /Users/joe/.ssh/id_rsa


# Client user (client)
Host github-client
HostName github.com
User git
IdentityFile /Users/joe/.ssh/id_rsa_client

You then use two corresponding remotes:

git clone git@github.com:joe/my_repo.git

and

git clone git@github-client:client/his_repo.git

If you just want to use two emails, you can just give each clone a separate .git/config with the desired [user] settings.