有没有办法在最终用户在 Git 中提交文件时向他们显示消息?

Is there a way to display a message to an end-user when they commit a file in Git?

当有人对我的 Git 存储库中的特定文件进行更改时,我想在用户通过 Git CLI 提交或推送时向他们显示一条消息。

场景:用户更改crazycalculations.py,我想说,"Hey, I notice you altered crazycalculations.py. Did you have John in Accounting review your change?"。只是对最终用户的一个友好提醒,他们弄乱了一个没有监督就不能弄乱的文件。

关于如何完成此任务有什么建议吗?

是的,您可以使用 git 钩子来做到这一点。如果您导航到 .git/hooks,您将看到 shell 代码示例,这些代码可以在特定 git 事件上 运行。检查 .git/hooks/pre-commit.sample 将有一些您可以使用的示例代码。

例如,在.git/hooks/pre-commit的顶部:

if git diff HEAD --name-only | grep -q "crazycalculations.py"; then
   echo "Are you sure you want to edit crazycalculations.py?"
   sleep 3
fi

文档:

https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks