Install the package in your Laravel project:
composer require --dev sweetchuck/git-hooks
Configure composer.json to point to your custom Git hooks directory:
"extra": {
"sweetchuck/git-hooks": {
"core.hooksPath": "./git-hooks",
"symlink": true
}
}
Create a git-hooks directory in your project root and add hook files (e.g., pre-commit, pre-push).
Example git-hooks/pre-commit:
#!/bin/sh
echo "Running pre-commit checks..."
# Add your logic here (e.g., PHPStan, Pest, etc.)
Trigger setup by running:
composer install
The package will symlink/copy hooks to .git/hooks/ automatically.
Use pre-commit to block invalid commits:
#!/bin/sh
php artisan test:unit
php artisan pest
git-hooks/pre-commit and commit the file to version control.git-hooks/ to share hooks across the team (e.g., pre-commit, pre-push).# git-hooks/pre-commit
php artisan migrate:status --no-interaction || exit 1
app/Console/Kernel.php to define reusable hook tasks:
protected function schedule(Schedule $schedule): void
{
$schedule->command('githook:pre-commit')->everyMinute();
}
git-hooks/ to the repo.core.hooksPath to override paths per environment (e.g., ./git-hooks/staging/).#!/bin/sh
echo "Hook triggered at $(date)" >> /tmp/git_hooks.log
Symlink Permissions:
.git/hooks/ is writable:
chmod -R u+w .git/hooks/
symlink in config if permissions fail:
"symlink": false
Git Version Mismatch:
core.hooksPath is ignored. Fall back to symlinks/copies.git --version
Hook Execution Order:
git-hooks/ run after built-in Git hooks (e.g., pre-commit runs last)..git/hooks/ directly (not recommended for team sharing)../git-hooks/pre-commit
ls -la .git/hooks/ | grep pre-commit
.git/hooks/pre-commit to bypass it during debugging../.git-hooks.sh to support non-Robo scripts (e.g., Node.js):
if command -v npm &> /dev/null; then
npm run lint
fi
config() to load hooks dynamically:
#!/bin/sh
HOOK_SCRIPT=$(php artisan config:get git-hooks.pre-commit)
eval "$HOOK_SCRIPT"
if [ -z "$CI" ]; then
# Run hook logic
fi
resources/git-hooks/ and copy them to git-hooks/ during setup.post-checkout hook to validate hook files:
#!/bin/sh
[ -f "git-hooks/pre-commit" ] || echo "ERROR: Missing pre-commit hook!" >&2
# git-hooks/pre-commit
if [ -f ".phpstan.cache" ]; then
php artisan phpstan
fi
How can I help you explore Laravel packages today?