如何制作一个git pre-commit钩子来检查提交消息? [英] How to make a git pre-commit hook that checks the commit message?
问题描述
我有一个git commit hook脚本,用于检查提交消息,如果消息中不包含单词"updated",则该脚本应拒绝提交.
I have a git commit hook script, that checks the commit message, and if the message does not contain the word "updated", the script should reject the commit.
#!/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
如果我尝试使用命令在本地存储库中推送一些文件,如何使此挂钩自动执行
How to make this hook automatically execute if I try to push some files in my local repo using the command
git commit -m "updated:something"
我的想法是使其不像运行此脚本进行提交",而是当您打开控制台并尝试进行提交并输入提交消息时,脚本将自动检查您的提交消息并通过或拒绝它.
My idea is to make it not like "run this script to do commit", but rather when you open the console and try to make a commit and entering the commit message, the script will check your commit message automatically and pass it or reject it.
推荐答案
以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
.
这篇关于如何制作一个git pre-commit钩子来检查提交消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!