Indefinite Studies

Handling multiple git identities

I have a problem. It's probably a common problem, but it's still a problem. I have multiple identities. There's personal me, who writes a silly blog and tinkers and tries things and has dozens of half-done projects. There's also work me, who is diligent and professional and who finishes everything he starts. Sometimes, both of us have to share a computer.

That sounds normal, I hear you say. What makes it a problem? Git config makes it a problem. Git config sits at three levels - system (applies to repositories for all users of the machine), user (applies to all repositories for the current user) and repository (applies to the current repository only). For a single-identity user, this is fine - user properties go in the user config, and never need to be thought about again. For multi-identity users, it's not so easy - either have two computer users, or have to set user properties on every repository individually.

Except no, of course that's not true. There's a solution.

Introducing conditional includes

Instead of having a single global config file for all your git repositories, you can create any number of config snippets and conditionally include them into your global config. There are a few different conditions that can be applied, but for now I'm only looking at gitdir, which allows us to include config based on the path of the local repository.

Read more about the possibilities on git-scm.com.

Setting up path-based user properties

If you haven't already, start by organising your repositories into a directory structure that separates your repositories by the identity you want to use when you work on them. For example, you could put all your personal projects under ~/dev/personal and all your work projects under ~/dev/work.

First, let's set up a default user. Run git config --global -l to see your global config. If it doesn't already include user.name and user.email properties, create them:


git config --global user.name "Your Name"
git config --global user.email "your_email@example.local"
    

Now, if you edit ~/.gitconfig in your favourite text editor, you'll see that you have a user section.

Create a git config folder:

mkdir -p ~/.config/git

Create a new file in that folder, e.g. `work.config`. Copy the user section from your global config into that new file, and change it to your work identity details.


[user]
    name = "Your Work Name"
    email = "your_work_email@work.local"
    

Now, edit your global config. At the bottom, under your default user section, add the conditional include.


[includeIf "gitdir:~/dev/work/"]
    path = ~/.config/git/work.config
    

With that in place, your work user details will be used for any repositories under ~/dev/work/, and your default user details will be used for repositories anywhere else. Ta da - multiple identitities! No more accidentally committing with the wrong details, and no more having to remember to set user details on every repository.

Further reading