Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I configure git so it does not guess user.email configuration settings?

Tags:

git

On my system I don't have the user.email git configuration value set at the global level, on purpose. Instead, I configure it individually in each sandbox. This is because I need to use different e-mail addresses for different projects.

Unfortunately I sometimes forget to configure the value when I create a new sandbox. In those cases, git just "guesses" a value based on information it gets from the environment. That leads to various problems, for example commits are not attributed to me on github, and I won't have much luck getting those commits with @localhost e-mail addresses attributed to me retroactively.

Is there a way to configure git to error out instead of guessing when I try to commit without a local or global user.email value configured?

like image 429
Marc Liyanage Avatar asked Nov 06 '13 20:11

Marc Liyanage


2 Answers

There is a config option for that now.

user.useConfigOnly

Instruct Git to avoid trying to guess defaults for user.email and user.name, and instead retrieve the values only from the configuration.

So just set this to true like this:

git config --global user.useConfigOnly true

And next time when you try to make a commit without user.email and user.name explicitly set you will get an error.

$ git commit

*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  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 email was given and auto-detection is disabled
like image 195
Sergei M Avatar answered Oct 26 '22 12:10

Sergei M


Use a pre-commit hook

You can use a pre-commit hook to prompt you to setup your project-specfic email address. For example:

#!/bin/sh

email=`git config user.email`

if ["$email" = ''] 
then
    echo "you need to set your email for this project"
    exit 1
fi

This will cause committing without the appropriate config to fail:

$ git commit -va
you need to set your email for this project
$

Use git's template to make sure it's always present

You can use git templates to make sure the hook is present in future repositories by default, by placing the hook in the templates folder:

-> tree /usr/share/git-core/templates/
/usr/share/git-core/templates/
├── branches
├── description
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-rebase.sample
│   └── update.sample
└── info
    └── exclude

The exact location of the templates folder may vary with OS/distribution.

For existing repositories - either create/copy the hook into place or if the git-core templates folder has been updated run git init to create the new hook file.

like image 24
AD7six Avatar answered Oct 26 '22 11:10

AD7six