2011-01-08 70 views
6

我使用多个Git远程存储库,每个需要不同的Git凭据(名称和邮件)。如何在Git中管理多个用户配置?

有没有解决方案,如脚本或最佳实践如何管理它们?

我知道“config --local”,但我不想每次手动设置这个变量。

+0

另请参阅[我可以在.gitconfig中为自己指定多个用户吗?](http:// stackoverflow。com/questions/4220416/can-i-specify-gitconfig -in-myself-in-gitconfig) – 2014-05-19 01:46:34

回答

10

它看起来像只是说在一个特定的仓库git config user.name(或user.email),而不指定--global--system会做的伎俩。默认设置是在当前的存储库中设置配置,并且必须为其提供明确的选项才能写入您的用户级别或系统范围的配置。

不知道如果你刚刚克隆需要不同配置的存储库,人们会怎么做。也许你可以编写一个包含git clone的小脚本来克隆一些存储库,然后根据任何信息设置适当的配置?如果您放弃/usr/lib/git-core中名为git-nclone的脚本,则可以将其作为git nclone运行。

编辑:既然你不想每次都手动设置它,那么克隆包装器如何记住你实际使用的各种集合,并且可以为你正在克隆的存储库选择合适的包装。它甚至可以根据您的克隆来自哪里进行智能默认设置。

+1

在.git/config文件中出现对本地配置的更改 – Abizern 2011-01-08 19:31:28

0

我创建了几个别名,看起来像这样:

的想法是,我可以救我的凭据(电子邮件,用户名)的别名定义。然后当我想克隆或初始化时,我不必每次都执行git config

初始化时:

initgithub = !git init && git config user.email [[email protected]] && git config user.name [yourgithubusername] 

initbitbucket = !git init && git config user.email [[email protected]] && git config user.name [yourbitbucketusername] 

克隆时:

clonegithub = "!f() { git clone $1 $2; cd $2; git config user.email [[email protected]]; git config user.name [yourgithubusername]; }; f" 

clonebitbucket = "!f() { git clone $1 $2; cd $2; git config user.email [[email protected]]; git config user.name [yourbitbucketusername]; }; f" 

ussage:

初始化时:

GIT中initgithub

GIT中initbitbucket

克隆时:

GIT中clonegithub https://github.com/pathtoproject.git/C /温度/ somefolder /项目

GIT中clonebitbucket https://github.com/pathtoproject.git/C /温度/ somefolder /项目

克隆时,基本上可以创建一个函数,该函数将执行正常的克隆操作和配置操作。目前,它要求您提供要克隆到的文件夹的路径,以便正确配置您的凭证。