代码中包含一些关键词的时候禁止git 提交,并输出日志。
增加文件 ./.git/hooks/pre-commit,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #!/bin/bash
# 检查 git 暂存区中的文件 files=$(git diff --cached --name-only)
# 遍历所有文件 for file in $files; do # 检查文件是否包含字符串 "tarry" if grep -qE "tarry|TARRY" "$file"; then echo "Error: The file '$file' contains the forbidden string 'tarry'." echo "Please remove the string before committing." exit 1 # 返回非零状态,阻止提交 fi done # 如果没有找到允许提交 exit 0
|