Home > Enterprise >  Can I set a username/email for all directories under some directory?
Can I set a username/email for all directories under some directory?

Time:11-23

So I have my global 'user.email' as "[email protected]". But I also have a folder that holds my work projects, organized like:

repos/
  workRepos/
     databaseMonitor/
     salesWebapp/
  sudokuSolver/
  someCoolGame/

I know I can set the local user.email on each sub-repo in workRepos/ to "[email protected]", but I'd rather set it directly inside workRepos/ so all projects underneath resolve my email to the company email, and I don't accidently submit something with the wrong identity.

I am hesitant to initialize a git repo in workRepos/, as some tools will show me the contents of that repo, instead of the one I'm working on. It seems like something git would have, to allow like a .git.settings file that is used if found anyone above the repo, to override the global settings.

From the SO recommended similar questions, I see that some config can be supplied in the global git config file, which would work, but I don't like complicating that, plus everything would break if I moved my repos to a new location, so it's not ideal, but probably what I'd do.

Is this the case? Any suggestions on doing what I want? Or am I stuck specifying this on a per-repo basis like some sort of Neanderthal?

CodePudding user response:

It's possible to do this using different .gitconfig files. My home directory looks like this:

~
-> .gitconfig
-> .gitconfig.personal
-> .gitconfig.work
-> /personal
-> /work

The contents of .gitconfig:

# default to work config
[include]
  path = ~/.gitconfig.work

# when working on personal projects
[includeIf "gitdir:**/personal/**/.git"]
  path = ~/.gitconfig.personal

[gpg]
    program = gpg
[commit]
    gpgSign = true

The contents of .gitconfig.work:

[user]
  name = Blair Nangle
  email = [email protected]
  signingkey = XXXXXXXXXXXXXXXX

The contents of .gitconfig.personal:

[user]
  name = Blair Nangle
  email = [email protected]
  signingkey = YYYYYYYYYYYYYYYY

Now, when I'm working on any Git projects that reside under ~/personal, my personal Git config will be used.

You can test your config by running git config user.email from ~/work/a-project and ~/personal/another-project.

  •  Tags:  
  • git
  • Related