【问题标题】:Prevent git from automatically generating user email if missing防止git在丢失时自动生成用户电子邮件
【发布时间】:2014-06-20 20:21:39
【问题描述】:

有没有办法将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.

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

【问题讨论】:

  • 您也许可以创建一个预提交挂钩来检查这一点,并在未配置用户名和电子邮件时让它中止。
  • 如果您自己解决问题,请考虑将其作为您自己问题的答案,以便未来的读者可以从该解决方案中受益。或者,考虑关闭和/或删除此问题。由你决定。

标签: git git-commit git-config


【解决方案1】:

设置git config --global user.useConfigOnly true

user.useConfigOnly

指示 Git 避免尝试猜测 user.email 和 user.name 的默认值,而是仅从 配置。例如,如果您有多个电子邮件地址并且 想为每个存储库使用不同的存储库,然后使用这个 配置选项在全局配置中设置为 true 以及 名称,Git 会在进行新提交之前提示您设置电子邮件 在新克隆的存储库中。默认为 false。

【讨论】:

    【解决方案2】:

    这可以通过git pre-commit hook 完成,遵循@cupcake 的建议。

    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
      
    2. 如有必要,使用chmod +x pre-commit使其可执行

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

      • 在 *nix 系统上,它位于

         /usr/share/git-core/templates/hooks
        
      • 在 Windows 上,这通常可以在

        中找到
         %ProgramFiles%\Git\share\git-core\templates\hooks
        
    4. 使用 git init 重新初始化现有的 git 存储库。

    【讨论】:

    • 在我将 #!/bin/sh 更改为 #!/bin/bash 之前,由于 [[: not found 错误而对我不起作用
    猜你喜欢
    • 1970-01-01
    • 2019-02-18
    • 2016-07-10
    • 1970-01-01
    • 2014-10-27
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多