git 提交指定消息时更改项目中的文件

Change file in project when git commit with specified message

当我使用指定的消息进行提交时,提供一些 git 挂钩更改项目文件的最佳方法是什么? 例如,当我执行 $git commit -m "MODIFY" 时,我想挂钩脚本以某种方式修改我的文件。修改文件没有问题。问题是如何使 git 挂钩将特定消息识别为修改触发器。

您可以使用 post-commit 挂钩。

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

After the entire commit process is completed, the post-commit hook runs. It doesn’t take any parameters, but you can easily get the last commit by running git log -1 HEAD. Generally, this script is used for notification or something similar.

下面是可以放在.git/hooks/post-commit中的示例。并确保将其设为可执行文件

#!/bin/bash
echo Running post-commit hook
git log -1 --pretty=oneline HEAD | grep "MODIFY" && touch somefile.txt

您可以通过 log -1 HEAD 获取最后的提交消息并通过 grep 传递它以匹配模式