git tip: multiple identities

If you are using git version control for personal and work repositories, it is tricky to change your email address (in ~/.gitconfig) to properly use the correct email before committing to a repo (this is what I do, depending on the nature of the repo: personal repo -> personal email, work repo -> work email).

Before this post, I was using some articulated methods to change my email address depending on the repo, but none of these methods was really portable (or officially documented).

Starting from version 2.8.0, you can use “multiple identities” when committing; to achieve this, you must remove your personal details from the config with:

git config --global --unset-all user.name

git config --global --unset-all user.email

and set a new option:

git config --global user.useConfigOnly true

Now, everytime you try to commit, git complains to instruct it with your personal details:

% git commit

*** Please tell me who you are.

Run

git config --global user.email "you@example.com" git config --global user.name "Your Name"

to set your account's default identity. Omit --global to set the identity only in this repository.

fatal: no name was given and auto-detection is disabled

The only drawback of this approach is that you have to set these details on every repository. Do you know any better ideas?

2 Replies to “git tip: multiple identities”

Leave a Reply