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

On my system, I do not have the user.email git configuration value set globally. Instead, I configure it separately in each sandbox. This is because I need to use different email addresses for different projects.

Unfortunately, I sometimes forget to adjust the value when creating a new isolated program. In these cases, git simply β€œguesses” the value based on the information received from the environment. This leads to various problems, for example, commits are not attributed to me on github, and I will not be able to get these commits with the @localhost email @localhost attributed to me retroactively.

Is there a way to configure git for an error, instead of guessing when I try to execute without the local or global value of user.email configured?

+8
git
source share
5 answers

Now there is a configuration option for this.

user.useConfigOnly

Configure Git to avoid trying to guess the default values ​​for user.email and user.name and instead get values ​​only from the configuration.

So, just set this to true as follows:

 git config --global user.useConfigOnly true 

And the next time you try to commit without explicitly setting user.email and user.name, you will get an error message.

 $ 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 default identity. Omit --global to set the identity only in this repository. fatal: no email was given and auto-detection is disabled 
+5
source share

Use pre-hook

You can use the pre-commit hook to suggest that you set up your email address specific to your project. 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 crash if there is no corresponding configuration:

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

Use the git template to make sure it is 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 template folder may vary depending on the OS / distribution.

For existing repositories - create / copy the hook in place or update the git -core templates folder, run git init to create a new hook file.

+3
source share

The preliminary solutions proposed here work well, and I agree with one of them. I ended up using this:

 if [ -z $(git config user.email) ]; then echo "You need to set your user.email configuration value for this project" echo "git config user.email foo@example.com" exit 1 fi 

It is activated system-wide in my ~/.gitconfig with the init.templatedir configuration init.templatedir , which I saw in another Stack Overflow post. I put my dir template on github as part of my git-tools .

In addition, I realized that I can just check on my command prompt prompt, which already adds git status to the prompt, to print a large, bold warning when I burn a CD to a sandbox that does not have user.email configured.

In my .bashrc :

 PS1='\u@\h:\W$(parse_git_branch) \$ ' parse_git_branch() { local DIRTY STATUS EMAIL_NOT_CONFIGURED_WARNING STATUS=$(git status 2>/dev/null) [ $? -eq 128 -o $? -eq 127 ] && return [ -z $(git config user.email) ] && EMAIL_NOT_CONFIGURED_WARNING=$' \x1b[31m(user.email not configured)\x1b[m' [[ "$STATUS" == *'working directory clean'* ]] || DIRTY=' *' echo "($(git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* //')$DIRTY)$EMAIL_NOT_CONFIGURED_WARNING" } 

This gives an easily visible warning:

enter image description here

+3
source share

It doesn't seem like it's possible. From git-commit-tree(1) :

  While parent object ids are provided on the command line, author and committer information is taken from the following environment variables, if set: GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE EMAIL (nb "<", ">" and "\n"s are stripped) In case (some of) these environment variables are not set, the information is taken from the configuration items user.name and user.email, or, if not present, system user name and the hostname used for outgoing mail (taken from /etc/mailname and falling back to the fully qualified hostname when that file does not exist). 

Perhaps it’s easier for you to set up environment variables for each sandboxed program?

+1
source share

That would be very hokey and possibly break extreme cases, but you could write a system-wide post-commit hook that looks at the author of the new commit and cancels it (via git reset ) if it seems fake.

Other than that, no, Git is happy to use any ridiculous email address that it binds together by default.

+1
source share

All Articles