til

Today I Learned: collection of notes, tips and tricks and stuff I learn from day to day working with computers and technology as a developer

View project on GitHub

Maintain Multiple Git Identities

If you like me use git for both work and play. It is nice to be able to separate Git identities.

Normally you would have something along the lines of:

# ~/.gitconfig

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

With release 2.13 of Git you can do conditional includes, which enables you to do:

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

So insert the above snippet into: ~/.gitconfig and create the file: ~/.gitconfig-work, just name stuff to match your preferences.

# ~/.gitconfig-work

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

Do note the order of things, so [user] should be before the overwrite specified by the conditional.

# ~/.gitconfig

[user]
    name = Firstname Lastname
    email = <private email address>
[includeIf "gitdir:~/develop/git-work/"]
    path = .gitconfig-work

After this you should be able to observe:

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

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

You can then specify all the conditional includes you want. My conditional includes configuration ~/.gitconfig looks like:

[includeIf "gitdir:~/develop/git-work/"]
    path = .gitconfig-work
[includeIf "gitdir:~/develop/gitlab-work/"]
    path = .gitconfig-work
[includeIf "gitdir:~/develop/github-work/"]
    path = .gitconfig-work

You should of course set the default to your primary/dominant repositories and then overwrite for the special repositories.

References