Git Hooks
Automatically run Ultracite on your staged Git files before committing.
Ultracite integrates with popular Git hook tools to automatically format and lint your code before every commit. This ensures all committed code follows your project's standards without manual intervention.
Setup
During initialization, you can select which Git hook tool to use:
npx ultracite init --integrations husky,lint-stagedOr select them interactively when prompted.
Supported Tools
Husky
Husky is a popular tool for managing Git hooks. Ultracite creates a .husky/pre-commit file that runs npx ultracite fix before each commit.
npx ultracite fixlefthook
lefthook is a fast Git hooks manager written in Go. Ultracite creates a lefthook.yml configuration:
pre-commit:
jobs:
- run: npx ultracite fix
stage_fixed: truelint-staged
lint-staged runs linters only on staged files. Ultracite adds configuration to your package.json or creates a .lintstagedrc file:
{
"lint-staged": {
"*.{js,jsx,ts,tsx,json,jsonc,css}": ["npx ultracite fix"]
}
}Note: lint-staged is typically used alongside Husky or lefthook to trigger the pre-commit hook.
pre-commit
pre-commit is a Python-based framework for managing Git hooks. Ultracite creates a .pre-commit-config.yaml:
repos:
- repo: local
hooks:
- id: ultracite
name: ultracite
entry: npx ultracite fix
language: system
types_or: [javascript, jsx, ts, tsx, json, css]
pass_filenames: falseAfter setup, run pre-commit install to activate the hooks.
How It Works
- You make changes and stage files with
git add - You run
git commit - The pre-commit hook runs
npx ultracite fix - Code is formatted and auto-fixable issues are resolved
- The commit proceeds with properly formatted code
Benefits
- Consistency: All committed code follows the same standards
- Automation: No need to remember to format code manually
- Clean History: Formatting issues never enter your repository
- Team Collaboration: Everyone follows the same rules automatically
Bypassing Hooks
In rare cases where you need to skip the pre-commit hook:
git commit --no-verifyUse this sparingly, as it bypasses the automated formatting.
How is this guide?