Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
同一项目文件夹中不同git存储库的多个全局用户名_Git - Fatal编程技术网

同一项目文件夹中不同git存储库的多个全局用户名

同一项目文件夹中不同git存储库的多个全局用户名,git,Git,我有两个存储库中心,如下所示: [includeIf "gitdir:~/checkouts/work/"] path = ~/.gitconfig.work [includeIf "gitdir:~/checkouts/personal/"] path = ~/.gitconfig.personal gitlab.com/和company\u internal.example.com/opensource/projects 在gitla

我有两个存储库中心,如下所示:

[includeIf "gitdir:~/checkouts/work/"]
    path = ~/.gitconfig.work
[includeIf "gitdir:~/checkouts/personal/"]
    path = ~/.gitconfig.personal
gitlab.com/
company\u internal.example.com/opensource/projects

在gitlab上,我使用
gitlab@example.com
使用用户名
公司名称
并在内部集线器上使用
my。name@example.com
迈·恩哈姆夫人

对于每个项目,我将在每个项目的
.git/config
中进行类似的设置:

[remote "upstream"]
    url = ssh://git@gitlab/foo/bar.git
    fetch = +refs/heads/*:refs/remotes/upstream/*
    name = Company Name
    email = gitlab@example.com

[remote "origin"]
    url = ssh://company_internal.example.com/opensource/projects/bar.git
    fetch = +refs/heads/*:refs/remotes/upstream/*
    name = Mrs. Mai Nhame
    email = my.name@example.com
然而,我必须为每一个人这样做。单身。项目还有很多项目。更不用说我在创建新项目时必须记住它。是否有办法根据git repo远程url全局设置不同的用户名/电子邮件


我见过的最接近的功能是基于本地目录进行更改,但在我的场景中,repos共享目录,不在单独的目录中。

Git不提供基于URL设置每个用户配置的方法。这是因为在进行提交时使用了
user.name
user.email
设置,Git不知道在进行提交时将这些提交推送到哪里(如果在任何地方)。你可以把他们推到一个地方,多个地方,或者任何地方,而且在任何情况下,这些决定都会发生在事实之后。请注意,这些选项控制您的个人名称(即其他人通常称呼您的名称)和电子邮件地址,但不控制连接到远程服务器或与远程设备进行其他交互的用户名

如果要根据磁盘上的位置配置某些设置,可以这样做:

[includeIf "gitdir:~/checkouts/work/"]
    path = ~/.gitconfig.work
[includeIf "gitdir:~/checkouts/personal/"]
    path = ~/.gitconfig.personal
然后,您可以更新这两个文件以包含这些目录中任何存储库的配置设置,包括
user.name
user.email


如果您想处理用于对远程服务器进行身份验证的多个用户名或凭据,那么Git常见问题解答将解释如何执行此操作,并且。

经过一些实验,我发现我可以执行的最接近的解决方案如下:

我创建了以下脚本:

gclone:

#!/bin/bash
git clone $1
it remote add upstream $2 $3 $4

我把它放在我的道路上。现在,不再使用
git克隆ssh://company_internal.example.com/opensource/projects/bar.git
然后尝试稍后修改配置,我做了
gclonessh://company_internal.example.com/opensource/projects/bar.git https://mrsmainhamerename@gitlab/foo/bar。但是,这只适用于支持url中的用户命名的基于https的存储库url,并且对我现有的repo没有帮助(仅适用于将来)。但至少是朝着正确的方向迈出了一步。

遥控器无法设置
user.name
user.email
选项。您最多可以为每个存储库设置它们。但是,如果您想控制用于推送的凭据,可以在每个远程基础上进行控制。谢谢。正如我的远程存储库配置示例所示,每个存储库的路由是我已经做过的。你知道有没有什么触发器可以在每次新的git克隆完成时触发脚本?不幸的是,根据标题,基于磁盘上的位置是我特别避免的标准之一。好吧,这就是git必须提供给你的旋钮。否则,答案是您想要做的是不可能的。刚刚发现了有用的git config值:
[credential”https://example.com“]username=me
我要看看它是否有效。