在一台计算机上的一个 Git 中的用户身份之间切换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9347458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switch between user identities in one Git on one computer
提问by Martin
I have ONErepository on GitHub, let's call it Repo-1
.
我在GitHub 上有一个存储库,我们称之为.Repo-1
I want to first access that repository as a default Git user.
我想首先以默认 Git 用户身份访问该存储库。
Let's call that user User-1
.
让我们打电话给那个用户User-1
。
I created SSH keypair, everything fine, works nice.
我创建了SSH 密钥对,一切正常,运行良好。
I made ANOTHERrepository on GitHub, let's call it Repo-2
.
我在GitHub 上创建了另一个存储库,我们称之为.Repo-2
I didn't make any changes in local Git, on my laptop. No configurational changes, nothing.
我没有在笔记本电脑上的本地 Git 中进行任何更改。没有配置变化,什么都没有。
Now - I want to clonefrom Repo-1
as the User-2
(but from the same laptop).
现在-我要克隆自Repo-1
为User-2
(但在同一台笔记本电脑)。
First of all: is this at all possible to do?
首先:这完全可能吗?
Can local Git on one single laptop switch between "user accounts" and present itself as User-2
? And then, from THAT identity, clone from Repo-1, make some change, and then push to Repo-1
?
一台笔记本电脑上的本地 Git 是否可以在“用户帐户”之间切换并显示为User-2
?然后,从那个身份,从 Repo-1 克隆,做一些改变,然后推送到Repo-1
?
If possible, how do I do that?
如果可能,我该怎么做?
采纳答案by Moe
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).
您已经在全局 .gitconfig 中配置了 SSH 密钥/用户信息。全局 .gitconfig 被本地 gitconfig 覆盖 - .git 文件夹中的文件“config”(如果它不存在,您可能需要创建它)。
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.
例如,您可以将 .gitconfig 文件复制到 .git 文件夹中(并将其重命名为“config”),然后只需更改要更改的行(可能是 github.user 和 github.token),或者您只需创建一个新文件其中的两行。
If you prefer the command line "git config" you can avoid all the file moving stuff by omitting the "--global" option.
如果您更喜欢命令行“git config”,则可以通过省略“--global”选项来避免所有文件移动内容。
回答by Matthew Flaschen
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.
您需要确定您是否真的有两个 ssh 密钥对,或者只是您想要使用的两个电子邮件。ssh 密钥对与此处所述的帐户相关联。
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.
ssh 密钥对(特别是私钥)基本上授予您的 git 客户端连接到 github 的权限,从而授予推送权限。这与用户身份是分开的,用户身份只是提交消息中的电子邮件。
If you have two ssh keypairs, each linked to one account, follow theseinstructions to create a ~/.ssh/config
file. The key part is to use a different ssh psuedo-host for each account:
如果您有两个 ssh 密钥对,每个都链接到一个帐户,请按照这些说明创建一个~/.ssh/config
文件。关键部分是为每个帐户使用不同的 ssh psuedo-host:
# 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 [email protected]: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.
如果您只想使用两封电子邮件,您可以为每个克隆单独.git/config
提供所需的[user]
设置。