DEV Community

loading...

Maintaining Different Git Identities

Max
Originally published at xam.io 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

[user]
    name = Firstname Lastname
    email = <private email address>

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

[user]
    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 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>
Enter fullscreen mode Exit fullscreen mode

Discussion (10)

Collapse
denji profile image
Denis Denisov • Edited
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";;
esac

.gitconfig

[init]
  templatedir = ~/.git-templates

Collapse
stanislas profile image
Stanislas

That's even better 🥺 Thanks

Collapse
12s12m profile image
12s12m

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

Collapse
plutov profile image
Alex Pliutau

Nice! Should it be changed?
From:

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

To:

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

Collapse
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.

Collapse
engineercoding profile image
Wesley Ameling

Thank you so much for this!

Collapse
vberlier profile image
Valentin Berlier

Thanks, that's awesome!

Collapse
patwoz profile image
Patrick Wozniak

This is amazing. Thank you!

Collapse
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

Collapse
maestromac profile image
Mac Siri

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