Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent git from automatically generating user email if missing

Is there a way to globally configure git to not automatically generate user's email, if none is set and abort the commit instead?

$ git commit -m "test"
[master (root-commit) 71c3e2e] test
Committer: unknown <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.

This can cause serious privacy leaks if the committer is not careful enough to check git warnings.

like image 604
the-happy-hippo Avatar asked May 04 '14 14:05

the-happy-hippo


People also ask

Can you use Git without email?

Git Config ⚙️Recent versions of Git don't allow to commit without having a name and email address configured for the repository. The email address is an integral part of a commit.

Why does git need my email?

So that someone else can know who committed the change. By the way, it is a simple matter to configure git with a default email address for your commits. It is explained in the Git SCM ebook: http://git-scm.com/book/en/v1/Getting-Started-First-Time-Git-Setup.

What is git config --'global user email?

Git allows you to set a global and per-project username and email address. You can set or change your git identity using the git config command. Changes only affect future commits. The name and email associated with the commits you made prior to the change are not affected.


2 Answers

Set git config --global user.useConfigOnly true

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. For example, if you have multiple email addresses and would like to use a different one for each repository, then with this configuration option set to true in the global config along with a name, Git will prompt you to set up an email before making new commits in a newly cloned repository. Defaults to false.

like image 78
laktak Avatar answered Sep 28 '22 16:09

laktak


This can be done with git pre-commit hook, following the suggestion by @cupcake.

  1. Create file named pre-commit like so:

    #!/bin/sh
    
    git config user.name >/dev/null 2>&1
    
    if [[ $? != 0 ]]; then
        echo "Error: user.name is undefined. Use 'git config user.name \"Your Name\"' to set it."
        exit 1
    fi
    
    git config user.email >/dev/null 2>&1
    
    if [[ $? != 0 ]]; then
        echo "Error: user.email is undefined. Use 'git config user.email [email protected]' to set it."
        exit 1
    fi
    
  2. If necessary, make it executable with chmod +x pre-commit

  3. Throw this file to global git hook templates:

    • On *nix systems this is located at

       /usr/share/git-core/templates/hooks
      
    • On Windows this can be typically found in

       %ProgramFiles%\Git\share\git-core\templates\hooks
      
  4. Re-initialize your existing git repos with git init.

like image 32
the-happy-hippo Avatar answered Sep 28 '22 16:09

the-happy-hippo