如果字符串出现在源代码(或差异)中,Git 在提交之前发出警告

标签 git

如果我要提交的更改包含特定字符串(例如,@todo@hack).

有人可以告诉我如何实现吗?

† 或警告。
‡ 或提交时。

最佳答案

检查字符串 '@todo' 是否被添加的简单预提交 Hook 可能如下所示:

#!/bin/sh

. git-sh-setup  # for die 
if git-diff-index -p -M --cached HEAD -- \
| grep '^+' \
| grep @todo; then
    die Blocking commit because string @todo detected in patch
fi

如果这是 .git/hooks/pre-commit 的内容并且是可执行的, 任何添加字符串 @todo 的补丁都将被拒绝。

关于如果字符串出现在源代码(或差异)中,Git 在提交之前发出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7291186/

相关文章:

Git:在 checkout 新分支之前没有提交分支

git commit --amend 是危险的

git - Cygwin:git 的颜色编码和分支信息?

Git日志,以特定颜色显示特定数据(通过正则表达式)

git - 如何在 Android Studio 中切换 git 帐户

Git clone - 创建一个裸克隆但带有远程跟踪分支(用于工作树 'main' repo )

github:全局更改 user.name 后访问被拒绝

git - 致力于掌握而不是开发

git - Azure DevOps 默认权限

git - Sourcetree 1.8.3 安装后首次登录时崩溃