如果丢失,防止git自动生成用户电子邮件

the*_*ppo 7 git git-config git-commit

有没有办法全局配置git不自动生成用户的电子邮件,如果没有设置并中止提交?

$ 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.
Run Code Online (Sandbox Code Playgroud)

如果提交者不够仔细检查git警告,这可能会导致严重的隐私泄露.

lak*_*tak 8

git config --global user.useConfigOnly true

user.useConfigOnly

指示 Git 避免尝试猜测 user.email 和 user.name 的默认值,而只从配置中检索值。例如,如果您有多个电子邮件地址,并且希望为每个存储库使用不同的电子邮件地址,则在全局配置中将此配置选项与名称一起设置为 true 时,Git 会提示您在创建新存储库之前设置一封电子邮件。提交到新克隆的存储库中。默认为 false。


the*_*ppo 1

这可以按照@cupcakegit的建议通过预提交挂钩来完成。

  1. 创建pre-commit如下名称的文件:

    #!/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
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如有必要,使其可执行chmod +x pre-commit

  3. 将此文件扔到全局git钩子模板中:

  4. git使用重新初始化您现有的存储库git init