多语言 Git 提交前检查框架
pre-commit 用 .pre-commit-config.yaml 管理 Git hooks,在提交前运行格式化、lint、类型检查等。每个 hook 在自己的隔离环境中运行,避免污染项目依赖。
pre-commit install
pre-commit install --hook-type commit-msg
pre-commit run --all-files
pre-commit run ruff --all-files
pre-commit autoupdate
pre-commit clean
pre-commit try-repo ../my-hooks --all-files
.pre-commit-config.yaml:
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.9
hooks:
- id: ruff
args: [--fix]
--all-files。rev 不固定会让不同时间安装出不同行为;定期 autoupdate 并审查。git add 再提交。commit-msg 钩子要单独 --hook-type 安装;只装默认 hook 不会生效。合上页面,先回答,再展开对照。
pre-commit run 默认没有检查整个仓库?pre-commit install 会安装 commit-msg 钩子吗?--all-files。git add 再提交,否则本次提交不包含修复。commit-msg 钩子要单独用 --hook-type commit-msg 安装。
小程序版
微信扫码,随时查命令