Marc Liyanage Marc Liyanage - 1 year ago 65
Git Question

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

On my system I don't have the
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
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
value configured?

Answer Source

Use a pre-commit hook

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


email=`git config`

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

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/
├── 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download