Run cargo fmt, clippy or any command as a git pre-commit hook, configured in Cargo.toml.
Website
·
Source
·
crates.io
pre-commit-hooks reads hook commands from Cargo.toml and runs them on git commit. It works with Cargo workspaces. It is a fork of the unmaintained pre-commit.
[dependencies]
pre-commit-hooks = "0.3"In a workspace, add it to one package, usually the main one. The hook applies to the whole repository.
Add a table like this to your root Cargo.toml:
[package.metadata.precommit]
fmt = "cargo fmt"
sort = "cargo sort"For a workspace:
[workspace.metadata.precommit]
fmt = "cargo fmt"
sort = "cargo sort -w"Then run:
cargo clean
cargo buildYou now have a pre-commit file in .git/hooks that runs the listed entries.
How do I run cargo fmt before every commit?
Add fmt = "cargo fmt" under [package.metadata.precommit], run cargo clean && cargo build, and the hook is installed.
Does it work with Cargo workspaces?
Yes. Use [workspace.metadata.precommit] in the root Cargo.toml and add the crate to one member package.
Is pre-commit-hooks maintained? Yes, by smbCloud. Releases are small and focus on keeping it working with current Cargo.
smbCloud is a cloud platform for small and medium businesses with Deploy, Auth, Mail and GresIQ. Enterprise power at startup velocity. Read the docs or install the CLI with cargo install smbcloud-cli.
Derived from rustation/pre-commit by Joe Grund. The find_crate_root function is refactored with the help of emilgardis.
© 2026 Seto Elkahfi (smbCloud). Distributed by Splitfire AB.