我有一个 git commit hook 脚本,它检查提交消息,如果消息不包含“更新”一词,脚本应该拒绝提交。
#!/bin/bash
read -p "Enter a commit message: " message
if [[ ${message} != *"updated"* ]];then
echo "Your commit message must contain the word 'updated'"
else
git commit -m "$message"
fi
如果我尝试使用命令将一些文件推送到我的本地仓库中,如何让这个钩子(Hook)自动执行
git commit -m "updated:something"
我的想法是让它不像“运行这个脚本来做提交”,而是当你打开控制台并尝试进行提交并输入提交消息时,脚本会自动检查你的提交消息并传递它或拒绝它。
最佳答案
以commit-msg
为例
#!/bin/bash
MSG="$1"
if ! grep -qE "updated" "$MSG";then
cat "$MSG"
echo "Your commit message must contain the word 'updated'"
exit 1
fi
chmod 755 commit-msg
并将其复制为 .git/hooks/commit-msg
。
关于bash - 如何制作检查提交消息的 git 预提交 Hook ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45416049/