Skip to content

Instantly share code, notes, and snippets.

@CodeBrotha
Last active May 2, 2022 21:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CodeBrotha/9eff39bd0ed916c69eb082d3673e2135 to your computer and use it in GitHub Desktop.
Save CodeBrotha/9eff39bd0ed916c69eb082d3673e2135 to your computer and use it in GitHub Desktop.
Using Git With Multiple Emails

Git 2.13 introduced conditional configuration includes. For now, the only supported condition is matching the filesystem path of the repository, but that’s exactly what we need in this case.

You can configure your conditional includes by adding them to the bottom of your home directory’s ~/.gitconfig file:

[includeIf "gitdir:~/work/"]
  path = ~/.gitconfig-work
[includeIf "gitdir:~/specific-project/"]
  path = ~/.gitconfig-specific-project

Now you can put whatever options you want into those files:

$ cat ~/.gitconfig-work
[user]
  name = Serious Q. Programmer
  email = serious.programmer@serious-business.com

$ cat ~/.gitconfig-specific-project
[user]
  name = John Q. Hacker
  email = lakerfan@somerandomemail.com

The appropriate config options will be applied automatically whenever you’re in a repository that’s inside your specified include directories. In any directories other than the specified include directories, Git will use your global config settings.

Your home directory's ~/.gitconfig file should now look something like this:

Mac/Linux:

[color]
  ui = true
[user]
  name = Your Name
  email = global@emailaddress.com
[includeIf "gitdir:~/work/"]
  path = .gitconfig-work
[includeIf "gitdir:~/specific-project/"]
  path = ~/.gitconfig-specific-project

Windows:

[color]
  ui = true
[core]
  autocrlf = true
[user]
  name = Your Name
  email = global@emailaddress.com
[includeIf "gitdir:C:/Users/<username>/work/"]
  path = C:/Users/<username>/.gitconfig-work
[includeIf "gitdir:C:/Users/<username>/specific-project/"]
  path = C:/Users/<username>/.gitconfig-specific-project

Windows Subsystem for Linux (if files are in Windows file system):

[color]
  ui = true
[core]
  autocrlf = input
[user]
  name = Your Name
  email = global@emailaddress.com
[includeIf "gitdir:/c/users/<username>/work/"]
  path = ~/.gitconfig-work
[includeIf "gitdir:/c/users/<username>/specific-project/"]
  path = ~/.gitconfig-specific-project

Navigate into a repo inside your "work" directory and run this command to check that it's working:

git config user.email
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment