This guide will help you integrate Husky (Git hooks manager) with Laravel Pint (PHP code style fixer) to automatically format your PHP code before every commit.
- Node.js and npm installed
- Composer installed
- Laravel project set up
Before starting, ensure you have:
- PHP 8.1+ with Composer installed
- Node.js 16+ with npm installed
- Git configured on your machine
- Laravel project already cloned/downloaded
Make sure your project has these files already created/copied
.husky/commit-msg
.husky/pre-commit
.husky/pre-push
commands/pre-commit
commands/pre-push
.editorconfig
.eslintignore
.eslintrc.js
β’gitignore
β’prettierignore
.prettierrc
commitlint.config.cjs
DEVELOPMENT-WORKFLOW.md
package-lock.json
package.json
pint.json
SETUP.md
Make sure you have laravle/plnt
exist in composer.json
, if not exist run this command
composer require laravel/pint --dev
Check package.json for husky
, lint-staged prettier
, eslint
, eslint-plugin-vue@next
, eslint-config-airbnb-base
, @commitlint/{cli,config-conventional}
, if not exist then run these commands selectively
npm install --save-dev husky lint-staged prettier
npm install --save-dev eslint eslint-plugin-vue@next
npx install-peerdeps --dev eslint-config-airbnb-base
npm install --save-dev @commitlint/{cli,config-conventional}
Run these commands to make sure all dependencies in PHP, Node.js are installed
# Install PHP dependencies (Laravel Pint is already included)
composer install
# Install Node.js dependencies (ESLint, Prettier, Husky, etc.)
npm install
# Setup Husky git hooks
npx husky init
# Check if hooks are installed
ls -la .husky/
# Should show:
# pre-commit
# pre-push
# commit-msg
# Check PHP code style (dry run)
npm run pint:check
# Fix PHP code style
npm run pint
# Check JavaScript files
npm run lint:check
# Fix JavaScript files
npm run lint
# Create a test branch (any name works now)
git checkout -b test-setup
# Try to commit (hooks will run automatically)
git add .
git commit -m "feat: test development workflow setup"
# If successful, you'll see:
# β
Running pre-commit checks...
# β
Branch name 'test-setup' is valid
# β
Pre-commit checks passed!
# β
[lint-staged output]
β
Running pre-commit checks...
β
Branch name 'test-setup' is valid
β
Pre-commit checks passed!
β
Running lint-staged...
β
Laravel Pint formatting files...
β
ESLint checking files...
β
Prettier formatting files...
β
Commit successful!
All branches are allowed, so you won't see branch-related errors anymore!
After setup, you can use these commands:
# PHP (Laravel Pint)
npm run pint # Fix PHP code style
npm run pint:check # Check PHP code style (dry run)
# JavaScript (ESLint + Prettier)
npm run lint # Fix JavaScript issues
npm run lint:check # Check JavaScript (dry run)
npm run format # Format with Prettier
npm run format:check # Check Prettier formatting
# Git Hooks
npx husky init # Reinstall git hooks
For better organization, consider using this pattern:
type/description-with-hyphens
Suggested examples:
feature/user-authentication
fix/login-validation-bug
chore/update-dependencies
docs/setup-instructions
Note: Branch naming is now optional - you can use any branch name you prefer.
# Reinstall hooks
rm -rf .husky
npx husky init
chmod +x .husky/pre-commit .husky/pre-push .husky/commit-msg
# Make sure dependencies are installed
npm install
composer install
# Make sure hooks are executable
chmod +x .husky/pre-commit .husky/pre-push .husky/commit-msg
chmod +x commands/pre-commit commands/pre-push
# Check if vendor/bin/pint exists
ls -la vendor/bin/pint
# If missing, reinstall Laravel Pint
composer require laravel/pint --dev
# Check ESLint configuration
npx eslint --print-config resources/js/app.js
# Reinstall if needed
npm install eslint eslint-config-airbnb-base eslint-plugin-import --save-dev
# If you see module loading errors, ensure commitlint.config.cjs exists
ls -la commitlint.config.cjs
# Reinstall commitlint if needed
npm install @commitlint/cli @commitlint/config-conventional --save-dev
You can now commit and push directly to any branch including:
main
master
develop
staging
production
Note: While direct commits are allowed, consider using feature branches for better collaboration.
Only use in emergencies:
git commit --no-verify -m "emergency fix"
git push --no-verify
After successful setup:
- Read the full guide: Check
DEVELOPMENT-WORKFLOW.md
for detailed workflow information - Create your first feature branch:
git checkout -b feature/your-feature-name
- Make some changes and test the commit process
- Share with your team: Have them follow this same setup process
Your development workflow is now active! Every commit will automatically:
- β Validate branch names
- β Format PHP code with Laravel Pint
- β Lint JavaScript with ESLint
- β Format code with Prettier
- β Validate commit messages
Happy coding with consistent, high-quality code! π