Maintaining Different Git Identities

maxlmator profile image Max Updated on ・2 min read

I like to have separate "identities" for my private and work stuff when using Git: Commits at work should be authored with my work email and commits in private projects with my private account. Until now I would always configure this per repository as soon as I noticed a commit done by the wrong email.

As I was setting up my new computer and edited the global .gitconfig, I wondered if there is a better way to keep this separate. Turns out there is one: With the release of version 2.13, Git introduced "Conditional Includes".

With these includes I can set a specific gitconfig-file to be include for all repositories within a specific location. As I store all my work projects within the folder ~/Work, I set the default user-config to be my private one and include the work-specific configfile for all Git repositories within that location.

# ~/.gitconfig

    name = Firstname Lastname
    email = <private email address>

[includeIf "gitdir:~/Work/"]
    path = .gitconfig-work
# ~/.gitconfig-work

    name = Lastname, Firstname
    email = <work email address>

The risk of being annoyed by wrongly associated commits is vastly smaller now — until I start checking out work projects to ~/Desktop/tmp for minor fixes.

To check your configuration, make sure to be in a directory which is tracked by Git. Non-Git directories will always show the default configuration:

$ cd ~/dev/justcurious 
$ git config user.email
<private email address>

$ cd ~/Work/projectA
$ git config user.email
<work email address>

$ cd ~/Work/not-a-repo
$ git config user.email
<private email address>


markdown guide
case "$url" in
  *@github.com:*  ) email="my-public@email";    name="public name";;
  *//github.com/* ) email="my-public@email";    name="public name";;
  *@corp.com:*    ) email="my-corporate@email"; name="real name";;
  *//corp.com/*   ) email="my-corporate@email"; name="real name";;


  templatedir = ~/.git-templates


That's even better 🥺 Thanks


Thanks this is really helpful. I used to manually edit the .git/config directory by appending the [user] information for every work related repo. This is a much better way :)


Nice! Should it be changed?

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


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


It's not necessary to specify the full path. The path is relative to the location of the .gitconfig file itself, therefore it works in this case both ways.

But to make it easier to understand, the fullpath is probably a better example.


Very useful! I used to set git config every time I cloned a repo, so this must save a lot of time and headaches! Thank you


Nice, this is super useful. I didn't even know I needed this. Thank you!