DEV Community


Maintaining Different Git Identities

Originally published at 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
Enter fullscreen mode Exit fullscreen mode
# ~/.gitconfig-work

    name = Lastname, Firstname
    email = <work email address>
Enter fullscreen mode Exit fullscreen mode

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
<private email address>

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

$ cd ~/Work/not-a-repo
$ git config
<private email address>
Enter fullscreen mode Exit fullscreen mode

Discussion (10)

denji profile image
Denis Denisov • Edited
case "$url" in
  **  ) email="my-public@email";    name="public name";;
  *//* ) email="my-public@email";    name="public name";;
  **    ) email="my-corporate@email"; name="real name";;
  *//*   ) email="my-corporate@email"; name="real name";;


  templatedir = ~/.git-templates

stanislas profile image

That's even better 🥺 Thanks

12s12m profile image

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 :)

plutov profile image
Alex Pliutau

Nice! Should it be changed?

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


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

maxlmator profile image
Max Author

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.

engineercoding profile image
Wesley Ameling

Thank you so much for this!

vberlier profile image
Valentin Berlier

Thanks, that's awesome!

patwoz profile image
Patrick Wozniak

This is amazing. Thank you!

cmmata profile image
Carles Mata

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

maestromac profile image
Mac Siri

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