Install pre-commit via pipx:
# Install pre-commit
pipx install pre-commit
# Verify installed version
pre-commit --versionIf pre-commit was installed via pipx, upgrade it with:
# Upgrade pre-commit
pipx upgrade pre-commit
# Verify installed version
pre-commit --versionInstall Git hook scripts:
# Install the pre-commit hook
pre-commit install
# Install the commit-msg hook
pre-commit install --hook-type commit-msg
# Validate pre-commit configuration
pre-commit validate-config
# Verify commit-msg hook (this does not create a commit)
tmp_commit_msg="$(mktemp)"
printf "chore(docs): test commit message\n" > "${tmp_commit_msg}"
pre-commit run conventional-pre-commit --hook-stage commit-msg --commit-msg-filename "${tmp_commit_msg}"
rm -f "${tmp_commit_msg}"Note:
- commit messages must follow Conventional Commits.
- Format:
<type>(<scope>): <description> - Example:
feat(core): add new validation check
- Format:
- The above commands avoid scanning the whole repository. If you want to run hooks against the whole repository, use:
pre-commit run --all-filesThis command only runs hooks in the pre-commit stage and may modify many files.
Update hook versions in .pre-commit-config.yaml:
pre-commit autoupdateAfter updating, prepare environments, run checks, and review changes before committing:
pre-commit install-hooks
pre-commit validate-config
tmp_commit_msg="$(mktemp)"
printf "chore(docs): test commit message\n" > "${tmp_commit_msg}"
pre-commit run conventional-pre-commit --hook-stage commit-msg --commit-msg-filename "${tmp_commit_msg}"
rm -f "${tmp_commit_msg}"
git diff .pre-commit-config.yamlUninstall Git hook scripts:
# Uninstall the commit-msg hook
pre-commit uninstall --hook-type commit-msg
# Uninstall the pre-commit hook
pre-commit uninstallUninstall pre-commit with:
pipx uninstall pre-commit