4

Is it possible to have different .gitconfig files at the root level of multiple folders? I use a different email address for work repositories and personal repositories, and it's difficult to always remember to set my email on each repository individually.

What I would like is something like the following:

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/
Christopher Haws
  • 1,715
  • 21
  • 21
  • Possible duplicate of https://stackoverflow.com/questions/4220416/can-i-specify-multiple-users-for-myself-in-gitconfig – Frodon Nov 27 '16 at 22:07

1 Answers1

4

In addition of answers mentioned in "Can I specify multiple users for myself in .gitconfig?", I prefer an alternative approach:

  • 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
    

When Git ask me who I am, I type either gcu or gcuvc depending on the nature of the Git remote repo (work or opensource)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I marked this answer as useful since it will work as a solution, but it seems to me like it's a pretty common thing for a person to want to use a different email for work and OSS. Is there really no build in mechanism to handle this? I feel like a simple solution to the problem is the one I detailed in my question. I have seen other software such as [NuGet](https://docs.nuget.org/ndocs/consume-packages/configuring-nuget-behavior#how-settings-are-applied) use the approach and it works very well. – Christopher Haws Nov 27 '16 at 22:46
  • I agree. I prefer this approach as I want to be sure I didn't mixed up those settings. I confirm them on a per-repo basis. – VonC Nov 27 '16 at 22:48
  • This seems like the most straightforward solution at this time, until git supports reading the directory tree up. It isn't that much of a burden. – MikeC Jan 14 '19 at 14:23