Use a different user.email and user.name for Git config based upon remote clone URL

The release of Git 2.13 has introduced a feature called conditional includes. In 2.13 the only supported configuration is filesystem path. That is easy to use in this case because I am already separating them.

The example provided on the release notes is:

You can configure two conditional includes in your home directory’s ~/.gitconfig file:

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

Now you can put whatever options you want into those files:

$ cat ~/.gitconfig-work
[user]
name = Serious Q. Programmer
email = serious.programmer@business.example.com

$ cat ~/.gitconfig-play
[user]
name = Random J. Hacker
email = rmsfan1979@example.com

Old answer

In Git 2.8, a global configuration user.useconfigonly has been added that insists the user set their user.email and user.name are set before committing. Here is the relevant text from the linked blog post by Github:

But if, say, you want Git to use one email address for your open source projects and a different one for your work projects, you’ve undoubtedly made the mistake of committing to a new Git repository without having first set your email address in that repository. In this situation, Git emits a warning, but it creates the commit anyway, using an email address that it guesses from the local system hostname. If you’re trying to do something as complicated as different addresses for different projects, this is almost certainly not what you want.

Now you can tell Git not to guess, but rather to insist that you set user.name and user.email explicitly before it will let you commit:

git config --global user.useconfigonly true

This doesn’t solve the auto-config based on certain clone URLs, but does make the process a little bit less error prone by forcing configuration at the start.

Leave a Comment

tech