我可以根据 URL 为 Git 存储库使用不同的作者信息吗

Can I use different author info for Git repo, depending on URL

在工作中,我们有Github Enterprise,但有时我也从publicGithub.com拉。我需要将我的作品作者信息用于任何 Github Enterprise activity,但我不想在更新我已有的 Github.com 存储库时将我的工作证书与我的个人证书混淆activity 上。


作为对比,我能够基于URL成功设置不同的代理信息如下:

git config --global http.https://github.com.proxy http://<myworkproxy:port>
git config --global https.https://github.com.proxy http://<myworkproxy:port>

仅当 https://github.com 位于远程 URL 时才会使用代理。

这个可以通过如下输入一个真实的远程URL来验证,Git会吐出要使用的代理:

git config --url-match https.proxy https://github.com/<rest of repo URL>

所以,我也对 user.nameuser.email 进行了上述操作:

git config --global user.https://github.com.name <My Github.com user name>
git config --global user.https://github.com.email <My Github.com user email>

这会检查我的全局配置文件(使用 git config --global -e 在编辑器中将其拉出),一切似乎都已正确设置:

[user]
    name = <My work user name>
    email = <My work user email>
[user "https://github.com"]
    name = <My github.com user name>
    email = <My github.com user email>

即使我使用 git config --url-match user.name https://github.com/<rest of repo URL>,它也会显示 github.com 用户名而不是工作用户名,因此过滤工作正常。

然而,当我使用 git 提交时(无论是从 GUI 还是直接在命令行上),它仍然总是使用我的工作 user.nameuser.email.

不可以URL全局匹配用户名和邮箱吗?还是我做错了什么?

如果需要,我可以直接更新每个存储库的名称和电子邮件,但我希望有一个更全球化的解决方案。

谢谢!

git 不允许您使用 URL 匹配来设置 user.nameuser.email 设置。

URL 匹配通常仅限于那些涉及与远程服务器交互的设置,例如 http.*credential.* 设置。如果你 运行 git config --help,你可以看到哪些设置允许这样做,因为它们在 <url> 组件中记录。

原因是 git 在设置非远程设置时不考虑克隆或推送 URLs。

但是,您可以在每个存储库的基础上进行设置。如果你想自动执行此操作,你可以在克隆时设置它们,方法是为每个用户设置一个带有适当配置文件的模板目录(参见 git init --help),并在克隆时使用 --template 选项。如果您愿意,可以将其推入别名。