Prevent git from automatically generating user email if missing

我是研究僧i 提交于 2019-12-21 12:39:11

问题


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 <my.user@my.very.own.hostname>
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.


回答1:


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.




回答2:


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 you@example.com' 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.



来源:https://stackoverflow.com/questions/23457116/prevent-git-from-automatically-generating-user-email-if-missing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!