可以为不同的文件夹使用不同的.gitconfig文件吗?

4

在多个文件夹的根目录下,是否可以拥有不同的.gitconfig文件?我在工作仓库和个人仓库中使用不同的电子邮件地址,但是每次都要单独设置每个仓库的电子邮件地址非常麻烦。

我想要的是类似以下的东西:

git/
├── work/
│   ├── .gitconfig (with user.email set to my work email)
│   ├── app1/
│   ├── app2/
└── home/
    ├── .gitconfig (with user.email set to my home email)
    ├── app3/
    └── app4/

可能是https://dev59.com/I2855IYBdhLWcg3wpmLw的重复问题。 - Frodon
1个回答

4

除了在“Can I specify multiple users for myself in .gitconfig?”中提到的答案外,我更喜欢另一种方法:

  • don't set any user configuration globally (no git config --global user.xxx)
  • force Git to ask me who I am at the first commit.
    This is possible with git 2.8+:

    git config --global user.useConfigOnly true
    
  • use an alias to set the right user setting.
    On Windows, I use:

    doskey gcu=git config user.name "My Name" ^&^& git config user.email my-email-for@work.com
    doskey gcuvc=git config user.name "VonC" ^&^& git config user.email my-email-for@opensource.org
    

当Git询问我是谁时,我会根据Git远程库的性质(工作或开源)输入gcugcuvc


我将此答案标记为有用,因为它可以作为解决方案,但我认为一个人想要在工作和OSS中使用不同的电子邮件似乎是一件相当普遍的事情。难道真的没有内置机制来处理这个问题吗?我觉得问题的一个简单解决方案就是我在问题中详细说明的那个。我已经看到其他软件如NuGet使用了这种方法,而且效果非常好。 - Christopher Haws
我同意。我更喜欢这种方法,因为我想确保我没有混淆那些设置。我会逐个存储库确认它们。 - VonC
这似乎是目前最直接的解决方案,直到git支持向上读取目录树。它并不算太麻烦。 - MikeC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接