强制git拒绝具有特定内容的提交

PGPoulsen

是否可以在我的代码中添加注释,以便GIT拒绝提交它?

原因是有时我正在研究两种不同的策略来解决问题,例如,两种不同的类。然后,如果可以在类中的某个位置添加注释,那就很好,// DONT COMMIT这样我就不会偶然提交两个并行的类。

缺氧

一个简单的预提交钩子可以完成任务:

#!/usr/bin/env bash


# The text that refuses the commit
# Change it to match your needs; keep it enclosed in single quotes
MARKER='// DONT COMMIT'


# Verify the staged files for the presence of the marker text
if git diff --cached --name-only | xargs grep -q "$MARKER"; then
  echo 'Cannot commit because the "no-commit" marker has been found in one of the staged files.' 1>&2
  # Refuse to commit
  exit 1
fi

将其放入.git/hooks/pre-commit项目中的文件中,并使该文件可执行(chmod +x .git/hooks/pre-commit)。

它只是一个存根,可能无法在所有情况下都按预期工作。扩展它以满足您的需求。

您可以通过命令行中传递选项-n(或--no-verify来告诉Git跳过钩子git commit

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章