自定义git行为增加commit检查
自定义git行为增加commit检查
当然,这种操作可以通过添加pre-commit
的钩子来实现,不过我采用了修改shell内容的实现方式
把这部分内容写到任何能够修改shell
行为的配置文件当中都行。
git() {
# 如果是 `git commit`,执行特殊检查
if [[ "$1" == "commit" ]]; then
force_mode=0
args=()
# 解析参数,检查是否有 --force
for arg in "$@"; do
if [[ "$arg" == "--force" ]]; then
force_mode=1
else
args+=("$arg")
fi
done
# 如果没有 --force,检查是否有未暂存的更改
if [[ "$force_mode" -eq 0 ]]; then
if git status | grep -q "Changes not staged for commit:"; then
echo "❌ 检测到未暂存的更改,请先运行 'git add' 暂存它们!"
echo "💡 或者使用 'git commit --force' 忽略此检查。"
return 1
fi
else
echo "⚠️ 强制提交,跳过未暂存更改的检查!"
fi
# 运行真正的 `git commit`
command git "${args[@]}"
else
# 对于其他所有 `git` 命令,直接执行
command git "$@"
fi
}
可以根据自己的需求来修改参数或者增加其他自定义行为。