基于用户名而不是主机来管理多个Github帐户

huangapple go评论72阅读模式
英文:

Multiple Github accounts based on user name instead of host

问题

我看到有建议使用多个GitHub帐户的方法是创建一个包含多个条目的SSH配置,如下所示:

Host aliasUser1
	HostName github.com
	User git
	IdentityFile ~/.ssh/user1

Host aliasUser2
	HostName github.com
	User git
	IdentityFile ~/.ssh/user2

这将允许我运行如下命令:

git clone aliasUser1:user1/repo.git
git clone aliasUser2:user2/repo.git

但我更喜欢配置,使我可以像平常一样运行命令,并根据组织/用户名进行区分,也就是说:
git clone git@github.com:user1/repo.git -> 将使用user1的密钥
git clone git@github.com:user2/repo.git -> 将使用user2的密钥

这种配置是否可能?

我目前不太确定应该尝试什么。

英文:

I saw the suggestion to work with multiple Github accounts is to create a SSH config with multiple entries like so:

Host aliasUser1
	HostName github.com
	User git
	IdentityFile ~/.ssh/user1

Host aliasUser2
	HostName github.com
	User git
	IdentityFile ~/.ssh/user2

This will allow me to run commands like so:

git clone aliasUser1:user1/repo.git
git clone aliasUser2:user2/repo.git

But I prefer to have a configuration that will allow me to run the commands as usual and make the differentiation based on the org/ user name, meaning:
git clone git@github.com:user1/repo.git -> will use user1 key
git clone git@github.com:user2/repo.git -> will use user2 key

is this configuration possible?

I'm not sure what to try currently

答案1

得分: 1

可以的,你可以这样做,只需稍作调整。Git拥有足够通用的URL重写配置,url.<realprefix>.insteadof

git config url.workme:user1/.insteadof git@github.com:user1/

从那时开始,Git会将git@github.com:user1/前缀重写为workme:user1/,然后将结果识别为(仍然)使用SSH;你需要配置你的SSH以将真实连接数据还原:

host workme
  hostname github.com
  user git
  identityfile ~/.ssh/user1.pub
英文:

Yes, you can do this, you have to do a little finagling. Git's got a general-enough url rewriting config, url.&lt;realprefix&gt;.insteadof.

git config url.workme:user1/.insteadof git@github.com:user1/

and from then on Git will rewrite the git@github.com:user1/ prefix to workme:user1/ and then recognize the result as (still) using ssh; you need to configure your ssh to put the real connection data back:

host workme
  hostname github.com
  user git
  identityfile ~/.ssh/user1.pub

huangapple
  • 本文由 发表于 2023年1月9日 06:05:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定