有没有办法全局配置
git
,使其不自动生成用户的电子邮件(如果未设置)并中止提交?
$ 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.
如果提交者不够仔细检查
git
警告,这可能会导致严重的隐私泄露。
设置
git config --global user.useConfigOnly true
用户.useConfigOnly
指示 Git 避免尝试猜测 user.email 和 user.name 的默认值,而只从 配置。例如,如果您有多个电子邮件地址并且 想要为每个存储库使用不同的存储库,然后使用此 配置选项在全局配置中设置为 true 以及 name,Git 会在进行新提交之前提示您设置电子邮件 在新克隆的存储库中。默认为 false。
这可以通过
git
预提交挂钩来完成,遵循 @cupcake 的建议。
创建名为
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 [email protected]' to set it."
exit 1
fi
如有必要,请使用
chmod +x pre-commit
将此文件扔到全局
git
钩子模板:
在 *nix 系统上,它位于
/usr/share/git-core/templates/hooks
在 Windows 上,这通常可以在
中找到 %ProgramFiles%\Git\share\git-core\templates\hooks
使用
git
重新初始化现有的 git init
存储库。执行以下命令来防止:
git config --global user.useConfigOnly true
甚至执行:
git config --system user.useConfigOnly true
默认情况下,git config
user.useConfigOnly
设置为false
,因此如果未设置,git将在提交时尝试自动配置user.name
和user.email
。
但是在某些平台(例如 Ubuntu)上,一个真正的行为是 git 不会自动配置
user.name
,即使你将 user.name
留空,如果你尝试提交,就会出现“身份未知”错误,所以大多数人根本不会满足这个自动配置的事情,直到你有一个回购集user.name
但不是user.email
(简而言之,git只会自动配置user.email
)。
我假设有很多像我这样的人只想全局配置
user.name
而不是user.email
以防止提交错误的电子邮件,并且绝对不想拥有自动配置的“电子邮件”东西,如果是这样,只是运行上面的命令。